return value of fread()

This is a discussion on return value of fread() within the C Programming forums, part of the General Programming Boards category; This is a continuation of my previous post, but since the problem in question has changed, I thought I'd make ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    20

    return value of fread()

    This is a continuation of my previous post, but since the problem in question has changed, I thought I'd make a new post.

    My original post was:
    storing text file contents into a string

    My new question pertains to itsme86's recommendation of using the return value of fread() to determine the size of a text file. I want to do this because I'm storing the contents of a text file into a string, and then analyzing the string. It makes more sence to have the size (string[SIZE]) of the string dynamically allocated, as opposed to me hardcoding a guess (string[512]) into my program.

    The problem I'm having is:
    I've looked at the man page for fread(), and it's still confusing to me. I'm not how to utilize itsme86's recommended code below, as provided from my previouse thread.

    Code:
    {
      size_t data_len;
    
      data_len = fread ( buf, 1, sizeof buf, in );
    }
    If someone could help me understand this better, I'd appreciate it, also I appreciate itsme86's previous attempts to help me.

    You guys rock for spending your time helping us newbs,
    Thank you,
    Michael

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The prototype for fread (from a google hit for "c function fread", http://www.opengroup.org/onlinepubs/...ons/fread.html) is
    Code:
    size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream);
    I think what itsme86 was suggesting was to make a large buffer, say
    Code:
    int buffer[100];
    and then try to read in the whole thing:
    Code:
    data_len = fread(buffer, 1, sizeof(buffer), in);
    If there aren't 100 integers in the file, fread will stop at the end of the file, returning how many ints there actually were in the file.

    You could use something other than ints, like a structure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    20
    I think i have more of a handle on that now.

    So using your example, if i open notepad, and type 12 numbers, such as "123412341234", then using data_len = fread(buffer, 1, sizeof(buffer), in); data_len would be set to 12?

    If so, how then, do I set the size of buffer to be 12 (buffer[12])?

    It looks to me like in order to do the fread(), you have to already have your string (buffer[]) defined, and if thats the case, I've already assigned a size to it, correct?

    Am I on the right track with this??

    Thanks for the help.
    Last edited by m.mixon; 07-23-2006 at 02:00 PM.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by m.mixon
    So using your example, if i open notepad, and type 12 numbers, such as "123412341234", then using data_len = fread(buffer, 1, sizeof(buffer), in); data_len would be set to 12?
    Well, it would be set to 12 if buffer was a char array. If it was an integer array, it would take the first four bytes (assuming 4 byte integers) and assign the first number to whatever "1234" represents *. data_len would then be 3, because fread was able to read three 4-byte integers from the file.

    * Since the numbers are stored in binary format in the file, "1234" in the file is not the same as the number 1234. Convert the byte sequence 1234 into binary and that's what the number would be. It's hard to create a file by hand to be read in by fread if you have data besides characters.

    Quote Originally Posted by m.mixon
    If so, how then, do I set the size of buffer to be 12 (buffer[12])?
    You mean, how can you make sizeof(buffer)==12? You can't really, unless you use realloc(). But you can use data_len whereever you would otherwise have used sizeof(buffer).

    Quote Originally Posted by m.mixon
    It looks to me like in order to do the fread(), you have to already have your string (buffer[]) defined, and if thats the case, I've already assigned a size to it, correct?
    Yes. It's a pretty simple way to do it, but not the best way. You'll waste some storage space and limit the number of records in the file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,170
    You could always do this:
    Code:
    {
      char *buffer = malloc(10), *tmp;
      unsigned int n_allocs = 1;
      size_t items_read;
    
      while(fread(buffer + ((n_allocs - 1) * 10), 1, 10, in) == 10)
      {
        n_allocs++;
        tmp = realloc(buffer, n_allocs * 10);
        if(!tmp)
        {
          // Handle memory allocation error
        }
        else
          buffer = tmp;
      }
    
      // Do stuff with buffer
    
      free(buffer);
    
    }
    That will read in the entire file 10 bytes at a time. The most space you'll be wasting is sizeof(*buffer) * 10 bytes. The number doesn't have to be 10. You can make it whatever you want, but the lower the number the more freads() there will be. And the more freads() there are, the less efficient it will be. But if you make the number higher then you'll run the risk of wasting more space.

    Also, the above code could use better error checking, but it serves as an algorithm example.
    Last edited by itsme86; 07-24-2006 at 06:31 PM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    > unless you use realloc().
    I think that's a little too complicated.

    You could always fseek() to the end of the file, use ftell to see how big it is, fseek() to the beginning, and read in the file.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  3. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM

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