fread not working

This is a discussion on fread not working within the C Programming forums, part of the General Programming Boards category; Hi guys, here I have a code snippet that I tried to run for 2 days without any result. There ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    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/200...itherspoon.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
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Since you're on DOS/Windows, try opening the file in "rb" mode.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by Salem View Post
    Since you're on DOS/Windows, try opening the file in "rb" mode.
    Thanks very much. Works like charm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread() and fwrite() - not working with gcc
    By senartcon in forum C Programming
    Replies: 12
    Last Post: 09-24-2011, 03:52 PM
  2. fread() not working
    By ahna in forum C Programming
    Replies: 5
    Last Post: 07-08-2011, 01:30 PM
  3. fread() + fwrite() not working properly
    By TaiL in forum C Programming
    Replies: 13
    Last Post: 02-24-2010, 10:29 PM
  4. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  5. Evul Fread() Not Working =(
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2002, 05:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21