Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what went wrong with my program?

Status
Not open for further replies.

Silentkiller

Technical User
Feb 13, 2003
14
0
0
GB
Hi,
I need some help, i have written a program in C to read the header, width , height and maxVal of a pgm files.
I managed to read and display the header information, but failed to read the width, height and maxVal of the file. Can anyone enlighten me on what went wrong.

Here is my program:
/* Pgm.c */

#include <stdio.h>
#include <stdlib.h>
#include &quot;pgm.h&quot;


// #define Max 2000

int main( PGMImage *img)
{
int ch;
FILE *fp;
int type;
char string[80];


printf(&quot;Please enter the filename :&quot;);
scanf(&quot;%s&quot;,string);


if ((fp = fopen(string, &quot;r&quot;))== NULL)
{
printf(&quot;Can'tOpen %s\n&quot;, string);
exit(1);
}

ch = getc(fp);
if (ch != 'P')
{
printf(&quot;ERROR(1) : Not Valid PGM File type\n&quot;);
exit(1);
}

printf(&quot;\n The filename is %s\n&quot;, string);

ch = getc(fp);
type = ch - 48;
if (( type != 2) && ( type != 5))
{
printf(&quot;EORROR(2): Not Valid PGM File type\n&quot;);
}

printf(&quot;\n The value is %ld\n&quot;, type);

fseek(fp, -1, SEEK_CUR);

fscanf(fp,&quot;%ld&quot;, &((*img).width));
fscanf(fp,&quot;%ld&quot;, &((*img).height));
fscanf(fp,&quot;%ld&quot;, &((*img).maxVal));

printf(&quot;\n width = %ld&quot;,(*img).width);
printf(&quot;\n height = %ld&quot;,(*img).height);
printf(&quot;\n maxVal = %ld&quot; ,(*img).maxVal);
printf(&quot;\n&quot;);

return (0);


}

here is pgm.h

#ifndef PGM_H
#define PGM_H

#define Max 2000

/*
#define LOW_VALUE 0
#define HIGH_VALUE 255
*/

struct PGMstructure
{
int maxVal;
int width;
int height;
};
typedef struct PGMstructure PGMImage;
#endif

thank
 
I think it would be helpful if you posted the content of the file, being especially sure to show all white space and newline characters. I suspect the problem is with way you are using 'fscanf'. If I were writing this I would probably use 'get' for all my reading and then parse each line as necessary with 'scanf'. I also don't see why you are using fseek, since it appears you are reading the file sequentially.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top