Like Tree4Likes
  • 2 Post By Salem
  • 2 Post By grumpy

how do I tell if fread returns a char array less than eight bytes?

This is a discussion on how do I tell if fread returns a char array less than eight bytes? within the C Programming forums, part of the General Programming Boards category; I do not understand how I can implement this. If fread != to at least 8 bytes then do THIS: ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    how do I tell if fread returns a char array less than eight bytes?

    I do not understand how I can implement this.
    If fread != to at least 8 bytes then do THIS:
    printf (" your file is near the end of file", fread result);
    Thanks
    Last edited by Once-ler; 03-01-2013 at 03:30 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    As per the definition of fread(), it returns the number of elements successfully read. The second argument is the size of each element. The third is the number of elements (the count) it attempts to read.

    If size is 1, and fread() returns a value other than the count, it has failed to read count characters. If its return value is equal to the count, it has succeeded.


    fread() does not tell you how many characters in the file remain waiting to be read though. It either succeeds in reading, or it doesn't, and reports that.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    well then how would I go about it? Everybody's been telling me to use fread but it seems not to be able to do what I need it to do.
    Thank you for your reply.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    How would I rig it so that it only reads 1 byte at a time and stores that in a buffer(which I have no idea how to do) untill it reaches 8 bytes or less and have something else tell me if buffer < 8 bytes?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
    So
    fread(buff,8,1,fp);
    will either return either 1 (success, you have 8 bytes) or 0 (you got nothing).

    This on the other hand
    fread(buff,1,8,fp);
    can return anything from 0 to 8 inclusive, and the return result is a direct count of the number of bytes read.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Essentially, you need to change your expectations. You're expecting to do what you want with a single fread() call. Instead, you need to put together a logical sequence of operations using fread() in various ways to achieve your desired end result.

    No, I'm not going to write code for you. The exercise is simple - as long as you don't expect to do it with only one call of fread().

    Similarly, if you can't work out how to copy a character - or a small number of characters - into a buffer, then you need to think more. That is a trivial exercise (although it can be done in more complex ways for specialised requirements).
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Thanks for the replies.
    I am not wanting someone to write the code for me, it's just that I am trying to understand what is going on here.
    Thanks you guys.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    Should I be using fscanf instead? And if so how could I implement it?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    It strikes me you are just flailing around, and making half-arsed guesses about how to do what you want, without really understanding either the goal or the tools (eg library functions) available to you. You need to be more systematic. Indeed, I'm prepared to bet that detecting the number of characters remaining in the input file is not a real requirement - you've made a guess at how to solve your real problem, and suddenly found that you can't make it work without detecting the number of characters remaining in the file.

    How do I know that? Detecting number of bytes remaining in a file is rarely a real requirement. And strange pseudo-requirements like yours usually result from a string of half-baked guesses at solving other problems.




    As to using scanf() as a miraculous magical alternative to using fread() to detect number of bytes remaining before end of file ..... you're not even close.

    At a high level, fscanf() behaves like fread() - it either succeeds in reading some or all of the requested data, or it fails, and that is reported using a return value. So the technique to do what you want would be essentially the same.

    The choice between fscanf() and fread() should be derived from requirements, such as having formatted (ostensibly human readable) input or unformatted (machine readable rather than human readable, probably more compact than a human-readable file). You have described no such requirements, and they are not relevant to the problem you have asked about (detecting number of bytes remaining to be read in the file).

    fscanf() also does things like (depending on the format string) ignoring whitespace and skipping other characters in the input file, so its return value is not necessarily related to the number of bytes it reads.
    Salem and AndiPersti like this.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sendto() returns wrong bytes sent
    By tmcp in forum C Programming
    Replies: 3
    Last Post: 03-09-2011, 12:43 AM
  2. fread returns strange values
    By seaking1 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 02:10 AM
  3. fread bytes of data from txt file: break on newline
    By 911help in forum C Programming
    Replies: 10
    Last Post: 02-24-2008, 09:30 AM
  4. a function that returns the number of bytes of a file
    By paulovitorbal in forum C Programming
    Replies: 22
    Last Post: 05-15-2006, 06:01 PM
  5. how do I declare a function that returns a char array
    By cxs00u in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2002, 10:38 AM

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