我想在兩個圖像讀取,然後輸出它們分成兩個多維數組,然後在後面的程序翻蓋/混合/輸出的圖像。有沒有人有任何想法,爲什麼我繼續得到分割錯誤?我知道這是ReadImages
電話中怎麼一回事,因爲它完全符合,然後當我執行它 - 第一個電話打進來,但之後失敗....分割錯誤/結構/指針
void ReadImages(struct ImageType *imgur, struct ImageType *imgur2)
{
int i = 0, j = 0;
char filename[30];
char filename2[30];
//Scanning in the first file.
FILE *inputfile;
fprintf(stdout, "Please enter the filename/location of the first image\n");
fscanf(stdin, "%c", &filename);
inputfile = fopen(filename, "r");
if(inputfile = NULL)
{
fprintf(stderr,"Sorry you didn't specify correctly\n");
}
fscanf(inputfile,"%[^\n]%c", imgur->ppImage, &imgur->newlinechar);
fscanf(inputfile,"%[^\n]%c", imgur->comment, &imgur->newlinechar);
fscanf(inputfile,"%i %i", &imgur->width, &imgur->height);
fscanf(inputfile,"%i", &imgur->maxColor);
for(i = imgur->height - 1; i >= 0; i--)
{
for(j = 0; j > imgur->width; j++)
{
fscanf(inputfile,"%i", &imgur->image[i][j].red);
fscanf(inputfile,"%i", &imgur->image[i][j].green);
fscanf(inputfile,"%i", &imgur->image[i][j].blue);
}
}
//Scanning in the second file.
FILE *inputfile2;
fprintf(stdout, "Please enter the filename/location of the second image\n");
fscanf(stdin, "%c", filename2);
inputfile2 = fopen(filename2, "r");
if(inputfile2 = NULL)
{
fprintf(stdout,"Sorry you didn't specify the filename/location correctly\n");
}
fscanf(inputfile2, "%[^\n]%c", imgur2->ppImage, &imgur2->newlinechar);
fscanf(inputfile2, "%[^\n]%c", imgur2->comment, &imgur2->newlinechar);
fscanf(inputfile2, "%i %i", &imgur2->width, &imgur2->height);
fscanf(inputfile2, "%i", &imgur2->maxColor);
for(i = imgur2->height - 1; i >= 0; i--)
{
for(j = 0; j > imgur->width; j++)
{
fscanf(inputfile2,"%i", &imgur2->image[i][j].red);
fscanf(inputfile2,"%i", &imgur2->image[i][j].green);
fscanf(inputfile2,"%i", &imgur2->image[i][j].blue);
}
}
}
'如果(inputfile2 = NULL)'是一個邏輯錯誤。 –
@BitFiddlingCodeMonkey是正確的 - 你需要''==因爲你是在效果設置文件指針爲NULL(所以當你使用它,你會崩潰)。 – Floris