title: Reading PGM Image in C (Not Reading File Correctly)
body: <p>I'm using files, <a href="http://sun.iwu.edu/~shelley/sie/zoo/journal/pgm.c.html" rel="nofollow">pgm.c</a> and <a href="http://sun.iwu.edu/~shelley/sie/zoo/journal/pgm.h.html" rel="nofollow">pgm.h</a> to read in a PGM image file, and then my program will manipulate the file (grayscale, rotation, etc).</p>
  
  <p>However, I can't get the file to read properly.  When I use:</p>
  
  <pre><code>   PGMImage* img = (PGMImage*)malloc(sizeof(PGMImage));
     char iName1[256];
     printf("Enter the filename of your PGM image:\n");
     scanf("%s", iName1);
     getPGMfile(iName1, &amp;img);
  </code></pre>
  
  <p>The console returns the type, width, height and maximum value  of the image.  However, the width is never correct.  It's usually 0 (and one time it was 56, way off) whether I'm using a .pgm file I found online or one I made myself.  I've even tried hard-coding the width of the image, but even so, it causes the program the crash.  The width should be read in pgm.c, under the getPGMfile function:</p>
  
  <pre><code>void getPGMfile (char filename[], PGMImage *img)
   {
     FILE *in_file;
     char ch;
     int row, col, type;
     int ch_int;
  
     in_file = fopen(filename, "r");
     if (in_file == NULL)
     {
       fprintf(stderr, "Error: Unable to open file %s\n\n", filename);
       exit(8);
     }
  
     printf("\nReading image file: %s\n", filename);
  
     /*determine pgm image type (only type three can be used)*/
     ch = getc(in_file);
     if(ch != 'P')
     {
        printf("ERROR(1): Not valid pgm/ppm file type\n");
        exit(1);
     }
     ch = getc(in_file);
     /*convert the one digit integer currently represented as a character to
       an integer(48 == '0')*/
     type = ch - 48;
     printf("Type: %d", type);
     if((type != 2) &amp;&amp; (type != 3) &amp;&amp; (type != 5) &amp;&amp; (type != 6))
     {
        printf("ERROR(2): Not valid pgm/ppm file type\n");
        exit(1);
     }
  
     while(getc(in_file) != '\n');             /* skip to end of line*/
  
     while (getc(in_file) == '#')              /* skip comment lines */
     {
       while (getc(in_file) != '\n');          /* skip to end of comment line */
     }
  
     /*there seems to be a difference between color and b/w.  This line is needed
       by b/w but doesn't effect color reading...*/
     fseek(in_file, -1, SEEK_CUR);             /* backup one character*/
  
     fscanf(in_file,"%d", &amp;((*img).width));
     fscanf(in_file,"%d", &amp;((*img).height));
     fscanf(in_file,"%d", &amp;((*img).maxVal));
  
     //I omitted the rest, but it can be seen in the link above
  </code></pre>
  
  <p>I have no idea what's causing these errors.  I'm using Eclipse and MinGW GCC on Windows 7 (64-bit).  My instructor couldn't help me solve this, so I'm hoping you can! :)</p>
  
  <p>To summarize: The width of the PGM image is always read incorrectly, anything done with the file afterwards causes the executable to stop working.  Hard-coding the width also causes the the executable to stop working.</p>