您好,我尝试编写一个 C 程序,该程序创建一个 800x800 像素的 PGM 文件,然后用 100x100 像素的交替黑白方块填充该文件。它编译得很好,执行时冻结,生成的PGM文件看起来只是一条细的交替黑白线。
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char* argv[])
{
/*Declaring variables for rows, columns, boardsize, squaresize and pgmData
array*/
int row, col,i,j;
int iBoardSize = 800, iSquareSize = 100;
int **iPgmData;
/*Allocate memory for the data*/
row = 800;
col = 800;
iPgmData = malloc(row * sizeof(int *));
for (i = 0; i < row;i++)
iPgmData[i] = malloc(col * sizeof(int));
/*Assign data to the array the desired result is an 8x8 checkboard of 100x100
pixel squares of alternating black and white.*/
for (row = 0; row < iBoardSize; row++){
for (col = 0; col < iBoardSize; col++){
if ((row / iSquareSize + col / iSquareSize ) %2 == 0)
iPgmData[row][col] = 0;
else
iPgmData[row][col] = 255;
}
}
/* Open the PGM file */
FILE* image = fopen(argv[1], "wb");
if (image == NULL){
fprintf(stderr, "Can't open output file %s!n", argv[1]);
exit(1);
}
/*Write the header*/
fprintf(image, "P2n%d %dn255n", iBoardSize, iBoardSize);
for (row = 0;row <= iBoardSize; row++){
for (col = 0;col <= iBoardSize; col++){
if (col < iBoardSize)
fprintf(image, "%d ", iPgmData[row][col]);
else
fprintf(image, "n");
}
}
/*Write pgmData*/
fwrite(iPgmData, 1, iBoardSize*iBoardSize*sizeof(int *), image);
/*Close the PGM*/
fclose(image);
/*Free allocated memory*/
free(iPgmData);
return 0;
}
我看到的问题:
使用越界数组索引
for (row = 0;row <= iBoardSize; row++){
^^^^^^^^^^^^^^^^ Wrong. Should be row < iBoardSize
for (col = 0;col <= iBoardSize; col++){
^^^^^^^^^^^^^^^^ Wrong. Should be col < iBoardSize
if (col < iBoardSize)
fprintf(image, "%d ", iPgmData[row][col]);
else
fprintf(image, "n");
}
}
该块可以简化为:
for (row = 0;row < iBoardSize; row++){
for (col = 0;col < iBoardSize; col++){
fprintf(image, "%d ", iPgmData[row][col]);
}
fprintf(image, "n");
}
致电fwrite
fwrite(iPgmData, 1, iBoardSize*iBoardSize*sizeof(int *), image);
仅当具有连续数据时,才能使用fwrite
。在你的情况下,你没有。我不清楚你希望从那个电话中实现什么。我删除了调用,输出文件是有效的 PGM 文件。
未正确free
分配的内存
你有
free(iPgmData);
这只会解除分配为指针分配的内存。它不会释放指针指向的内存。您需要使用:
for (i = 0; i < row;i++)
free(iPgmData[i]);
free(iPgmData);
发布的程序过于复杂,并且包含有关索引到数组以及如何将数据写入输出文件的几个问题。
以下代码合并注释,执行适当的错误检查并生成所需的图像文件。
#include <stdio.h> // fopen(), fclose(), fprintf(), FILE
#include <stdlib.h>
#include <errno.h> // errno
#include <string.h> // strerror()
#define MAX_ROWS (800)
#define MAX_COLS (800)
#define BOARD_SIZE (800)
#define SQUARE_SIZE (100)
int main (int argc, char* argv[])
{
int row; // image row index
int col; // image column index
if( 2 != argc )
{ // then required command line parameter missing
fprintf( stderr, "USAGE: %s <PGM_filename>n", argv[0]);
exit( EXIT_FAILURE );
}
// implied else, correct number of command line parameters
/* Open the PGM file */
FILE* image = fopen(argv[1], "wb");
if (!image)
{
fprintf(stderr, "Can't open output file %s for write, due to: %sn", argv[1], strerror( errno ) );
exit( EXIT_FAILURE);
}
/*Write the header*/
fprintf(image, "P5n%d %dn255n", BOARD_SIZE, BOARD_SIZE);
/* write the gray scale image */
for (row = 0;row < BOARD_SIZE; row++)
{
for (col = 0;col < BOARD_SIZE; col++)
{
if( (row/SQUARE_SIZE)%2 == 0 )
{
if( (col/SQUARE_SIZE)%2 == 0 )
{
fprintf( image, "%c", 255 );
}
else
{
fprintf( image, "%c", 0 );
}
}
else
{
if( (col/SQUARE_SIZE)%2 == 0 )
{
fprintf( image, "%c", 0 );
}
else
{
fprintf( image, "%c", 255 );
}
}
}
}
fclose(image);
return 0;
} // end function: main