title: Reading a P2 .pgm image into a 2D array in C
body: <p>I have started learning about image processing and I am trying to read a .pgm file into a 2D array in C. I am testing the input and output, but the program did not work properly so, I tried  to make some changes. How could I improve/modify the code to make it work?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  struct PGMstructure 
  {
      int maxVal;
      int width;
      int height;
      int data[800][800];
  };
  
  
  int main()
  {
      FILE *imagein,*imageout;
  
      int row, col;
  
      int i,j;
      int ch_int;
      struct PGMstructure *imginfo;   
      char infpath[500],outfpath[500];
  
      printf("Enter PGM file path:");
      scanf("%s",infpath);
      imagein = fopen(infpath,"r+");
  
      if(imagein == NULL)
      {
          printf("Error opening first file");
          exit(8);
      }
  
      while(getc(imagein) != '\n');             
  
      while (getc(imagein) == '#')              
      {
          while (getc(imagein) != '\n');          
      }
  
      fseek(imagein, -1, SEEK_CUR);
      fscanf(imagein,"%d", &amp;imginfo-&gt;width);
      fscanf(imagein,"%d", &amp;imginfo-&gt;height);
      fscanf(imagein,"%d", &amp;imginfo-&gt;maxVal);
      printf("\n width  = %d\n",imginfo-&gt;width);
      printf("\n height = %d\n",imginfo-&gt;height);
      printf("\n maxVal = %d\n",imginfo-&gt;maxVal);
  
      for (row=0; row&lt;imginfo-&gt;height; row++){
  
          for (col=0; col &lt; imginfo-&gt;width; col++)
          {    
              fscanf(imagein,"%d", &amp;ch_int);
              imginfo-&gt;data[row][col] = ch_int;
          }
      }
  
      printf("Enter path of output file:");
  
      scanf("%s",outfpath);
      imageout = fopen(outfpath,"w+");
  
  
      for ( i = 0 ; i &lt; row ; i++ )
      {
          for ( j = 0 ; j &lt; col ; j++ )
          {
              fprintf( imageout,"%d" , imginfo-&gt;data[row][col] );
          }
          printf( "\n" ) ;
      }
  
      return 0;
  }
  </code></pre>