Problem reading file.

This is a discussion on Problem reading file. within the C Programming forums, part of the General Programming Boards category; I'm having a problem to read a file and put have all the content in a unsigned char pointer. All ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    97

    Problem reading file.

    I'm having a problem to read a file and put have all the content in a unsigned char pointer. All the content of the file is there, the problem is that it also adds some garbage at the end of the file. Here is the code:

    Code:
    unsigned char* ReadVPfile(char *filename)
    {
    	FILE * file;
    	long vpfileSize;
    
    	file=fopen(filename, "rb");
    
    	if(!file)
    		return false;
    
    	fseek(file, 0, SEEK_END);
    	vpfileSize = ftell(file);
    	rewind(file);
    
    	//allocate memory
    	unsigned char * vpfileText=new unsigned char[vpfileSize];
    	if(!vpfileText)
    		return false;
    
    	fread(vpfileText, 1, vpfileSize, file);
    
    	return vpfileText;
    }
    I hope any of you can help me. Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >unsigned char* ReadVPfile
    >char *filename
    >FILE * file
    Um...pick a style and go with it. Inconsistency makes your code harder to read. You've already covered the board in pointer declaration styles, and while that certainly stops all controversy, it also effects the clarity of your code. In fact, it looks like several people write it as a collaborative effort but couldn't make up their minds.

    >return false;
    false is not a pointer to unsigned char. If you want to return a null pointer then say NULL or 0, k? This is even more important in C89 where false is not a keyword.

    >fseek(file, 0, SEEK_END);
    >vpfileSize = ftell(file);
    This is not a reliable way to get the size of a file. The best way is to read it. Start at the beginning and read to the end. While you're at it you can resize vpfileText to hold the new data. At EOF, you can be reasonably sure that everything is peachy.

    >//allocate memory
    Unless you're using C99, this is a nonstandard extension. I know you're not using C99 because new isn't an allocation method in C99.

    >unsigned char * vpfileText=new unsigned char[vpfileSize];
    This is not C, it's C++. 1) new is not a way to allocate memory in C and 2) C89 does not allow declarations except at the beginning of a block. C99 fixes this, but since you're obviously compiling as C++, I can safely assume you didn't intend C99.

    >fread(vpfileText, 1, vpfileSize, file);
    >return vpfileText;
    If you intend to use vpfileText as a string then you'll get garbage unless you append a null character at the end:
    Code:
    unsigned char *ReadVPfile(char *filename)
    {
      FILE *file;
      long vpfileSize;
      unsigned char *vpfileText;
    
      file = fopen(filename, "rb");
      if(!file)
        return NULL;
    
      fseek(file, 0, SEEK_END);
      vpfileSize = ftell(file);
    
      rewind(file);
    
      vpfileText = malloc(vpfileSize + 1);
      if(!vpfileText) {
        fclose(file);
        return NULL;
      }
    
      fread(vpfileText, 1, vpfileSize, file);
      vpfileText[vpfileSize] = '\0';
      fclose(file);
    
      return vpfileText;
    }
    You also forgot to close the file before the function returns. And error checking for fread would be a good thing.
    My best code is written with the delete key.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    And the master responds. . I've been sitting here staring at his code
    Code:
    //allocate memory
    	unsigned char * vpfileText=new unsigned char[vpfileSize]
    trying to rack my brain about this line as to what he was trying to do. I've never done c++ so I wouldn't know. I ignored the OP coding style, but that had me. Then again i've only recently finished C and now learning to apply it, so this was a good thread to 'C'.

    [edit] is it really necessary to declare char * to unsigned? [/edit]

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    97
    Quote Originally Posted by Prelude
    >fseek(file, 0, SEEK_END);
    >vpfileSize = ftell(file);
    This is not a reliable way to get the size of a file. The best way is to read it. Start at the beginning and read to the end. While you're at it you can resize vpfileText to hold the new data. At EOF, you can be reasonably sure that everything is peachy.
    In which cases will this way fail to give you the correct size?
    Also, thanks for the comments!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In which cases will this way fail to give you the correct size?
    In any case. The C standard states explicitly that fseek calls on a binary stream with SEEK_END as the last argument need not be meaningfully supported.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Can you quote the relevant portion of the standard that relates to that Prelude? I've never heard of fseek() being unreliable when it comes to calculating a file size (except when size is > 2GB of course), and I'd like to learn more.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can you quote the relevant portion of the standard that relates to that Prelude?
    Sure:
    Code:
    7.19.9.2 The fseek function
    
    Synopsis
    
        #include <stdio.h>
        int fseek(FILE *stream, long int offset, int whence);
    
    Description
    
    The fseek function sets the file position indicator for the stream pointed to by stream.
    If a read or write error occurs, the error indicator for the stream is set and fseek fails.
    
    For a binary stream, the new position, measured in characters from the beginning of the
    file, is obtained by adding offset to the position specified by whence. The specified
    position is the beginning of the file if whence is SEEK_SET, the current value of the file
    position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not
    meaningfully support fseek calls with a whence value of SEEK_END.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Thanks

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by Prelude
    This is not a reliable way to get the size of a file. The best way is to read it. Start at the beginning and read to the end.
    Just curious, is stat() not reliable?
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by itsme86
    Just curious, is stat() not reliable?
    I don't believe
    stat is standard. I think prelude was trying to get across the more standard compliant way.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Well, it's POSIX standard at least But I guess you're right.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just curious, is stat() not reliable?
    It depends on what you want and what else is running. If another process is writing to the file then stat won't work very well. The biggest problem with stat is that it's not portable past POSIX systems. If that isn't a problem then by all means use it as the result is accurate.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Problem reading file
    By winsonlee in forum C Programming
    Replies: 2
    Last Post: 04-23-2004, 06:52 AM

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