0

Hi guys, here I have a code snippet that I tried to run for 2 days without any result. There is something wrong with fread function. When debugging, it doesn't read all the file that I need

Here is the link to the file http://www.math.umbc.edu/~rouben/2003-09-math625/images/reese_witherspoon.pgm

In main:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>

infilename = "C:cannyhara.pgm";


    if(read_pgm_image(infilename, &image, &rows, &cols) == 0){
          fprintf(stderr, "Error reading the input image, %s.n", infilename);
          getchar();
          exit(1);
       }

The function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int read_pgm_image(char *infilename, unsigned char **image, int *rows,
    int *cols)
{
   FILE *fp;
   char buf[71];
   size_t dummy;

   /***************************************************************************
   * Open the input image file for reading if a filename was given. If no
   * filename was provided, set fp to read from standard input.
   ***************************************************************************/
   if(infilename == NULL) fp = stdin;
   else{
      if((fp = fopen(infilename, "r")) == NULL){
         fprintf(stderr, "Error reading the file %s in read_pgm_image().n",
            infilename);
         return(0);
      }
   }

   /***************************************************************************
   * Verify that the image is in PGM format, read in the number of columns
   * and rows in the image and scan past all of the header information.
   ***************************************************************************/
   fgets(buf, 70, fp);
   if(strncmp(buf,"P5",2) != 0){
      fprintf(stderr, "The file %s is not in PGM format in ", infilename);
      fprintf(stderr, "read_pgm_image().n");
      if(fp != stdin) fclose(fp);
      return(0);
   }
   do{ fgets(buf, 70, fp); }while(buf[0] == '#');  /* skip all comment lines */
   sscanf(buf, "%d %d", cols, rows);
   do{ fgets(buf, 70, fp); }while(buf[0] == '#');  /* skip all comment lines */

   /***************************************************************************
   * Allocate memory to store the image then read the image from the file.
   ***************************************************************************/
   if(((*image) = (unsigned char *) malloc((*rows)*(*cols))) == NULL){
      fprintf(stderr, "Memory allocation failure in read_pgm_image().n");
      if(fp != stdin) fclose(fp);
      return(0);
   }
   dummy = fread((*image), (*cols), (*rows), fp);

   //ERROR OCCURS HERE, FREAD DOESN'T RETURN CORRECT VALUE (NOT EQUAL TO *row)

   if((*rows) != dummy){
      fprintf(stderr, "Error reading the image data in read_pgm_image().n");
      if(fp != stdin) fclose(fp);
      free((*image));
      return(0);
   }

   if(fp != stdin) fclose(fp);
   return(1);
}

Appreciate any help. I don't know if this this a trivial problem or not. many thanks

Edited by chipbu

2
Contributors
1
Reply
3
Views
4 Years
Discussion Span
Last Post by BobS0327
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.