Search code examples
cioscanfprintfppm

Won't Read in My Image


I am reading in a PPM file, and I have used printf's throughout the function to see if it will print but for some reason it prints the p3/comment/width/height/maxcolor but it wont print the pixels... I tried checking via using printf statements inside and outside my nested for loops but it wont read in the data...Any ideas??

void ReadImages(struct ImageType *imgur, struct ImageType *imgur2)
   {
     int i=0, j=0;
     char filename[30];
     char filename2[30];

     FILE *inputfile;
     fprintf(stdout, "Please enter the filename/location of the first image\n");
     fscanf(stdin, "%s", filename);
     inputfile = fopen(filename, "r");

     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);
               }
        }

Yes I have made sure that my struct is int red/green/blue and I have checked on printing out the comment/maxcolor/and everything else that all works.


Solution

  • Have you allocated storage for your image at imgur->image? If not, then your program is crashing at that point.

    Also, you do realize there's no print statements in your for loop that reads the pixels, so that could also be why it's not printing out the pixels.

    Edit: Aha, this is likely it:

           for(j=0; j > imgur->width; j++)
    

    Your comparison is backward. You want:

           for(j=0; j < imgur->width; j++)