fread woes

This is a discussion on fread woes within the C Programming forums, part of the General Programming Boards category; hi, im trying to use fread() to read from a binary file and there is an incredibly annoying problem. It ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    63

    fread woes

    hi, im trying to use fread() to read from a binary file and there is an incredibly annoying problem. It will read the first 100 or so bytes correctly, then the file just closes for no apparent reason. Well i assume its closing, calls to fread are returning 0. I never call fclose(), infact i havent even got around to calling it yet. Is there anything else that could be causing the error. The file is about 150kb so i highly doubt im reaching the EOF.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    Did you specifically open it in binary 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
    Jul 2006
    Posts
    63
    no i but i just googled that and it did solve the problem, lol first time with fileio. Anyway, whats the difference?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A "text" file (as in a "non-binary" file), the content is interpreted with "special rules", which means that a "end-of-file" is part of the interpreted content. This goes WAY back to when CP/M was the operating system for what would eventually become a PC [and other similar age operating systems]. CP/M only knew about disk-blocks, and a file may of course be "any number of bytes", not a multiple of 128, 512 or 1024 byte blocks. So a "end of file" character is part of the text file. This has stayed around since then.

    Unix filesystems have, since the beginning had "byte-sized filesizes", so you can tell exactly how long the file is, rather than in blocks. So there it won't make a difference.

    I bet that if you look at your file in a hex editor or some similar, you will find a "0x1A" at the point where it stops! This is the CTRL-Z character, which is the "end-of-file" character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    thx for the explanation, as part of the younger generation a little history is always interesting.
    And well it doesnt end in an A1, so does this mean im reading a binary or a text file lol?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, the point is that the fread() of a text file would stop at (before, really) a "1A" - because that indicates that it's the end of the file. So a text file may contain
    "Hello, World\n\x1a>" where the "\x1a" is the "end of file" character. Are you saying that this is not the case?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    63
    ok i get what ur saying. Thx for the help, both of you
    its a binary file, so yea it just terminates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. fread item_size limit
    By nvoigt in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2005, 08:08 AM

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