About reading PGM file

input pgm name to read the width height and max grayscale

and i got some long wrong value like
Image Width=2359264
Image Height=2009055277
Image Grayscale MAX=2358936



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int readPGM (char * inFileName, int * wIm, int * hIm, int * maxIm  );

int main()
{       printf("Please enter the <P2> PGM image file name:")	;
	int  wIm, hIm, maxIm;
	char inFilename [81];
	gets(inFilename);
	readPGM (inFilename, &wIm, &hIm, &maxIm  );
	printf("Image Width = %d\n",wIm);
	printf("Image Height = %d\n",hIm);
	printf("Image Grayscale MAX = %d",maxIm);

return 0;
}

int readPGM (char * inFileName, int * wIm, int * hIm, int * maxIm  );

if (inFileName == NULL) 
{
	printf("ERROR: inFileName == NULL!!\n");
	return 0;
}
	
	
	int w, h, max;
	FILE * inFS = fopen(inFileName, "r");
	char tmpStr[50];  
	
	fscanf (inFS, "%s", tmpStr);
	fscanf (inFS, "%d%d%d", &w, &h, &max);  
	
	
	*wIm = w; *hIm = h; *maxIm = max;
	
	fclose (inFS);
	return 1;


What does the input file contain?
Did you allow for comment lines beginning with '#'?

Also, there is no check whether the file inFS has been successfully opened.
Last edited on
Topic archived. No new replies allowed.