#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Fread Not Working


    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:

    Code:
    #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:

    Code:
    #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
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,367
    Rep Power
    1870
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    fread almost certainly works according to the manual. fread isn't broken. Start there.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo