Like Tree1Likes
  • 1 Post By Barney McGrew

so fread dosent have to have struct in its arguments the book I was reading said it -

This is a discussion on so fread dosent have to have struct in its arguments the book I was reading said it - within the C Programming forums, part of the General Programming Boards category; did [code]fread (&ch, sizeof(int),1, fp); [\code] what could the ,1 symbolize? and so I plug in my (unsigned long long ...

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

    so fread dosent have to have struct in its arguments the book I was reading said it -


    did
    [code]fread
    (&ch, sizeof(int),1, fp);
    [\code]
    what could the ,1 symbolize?
    and so I plug in my (unsigned long long int) before size of
    Am I right?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700

    Lightbulb Rtfm!

    First locate a good C library reference (there are plenty available online) and read up on the usage notes for fread. Post back if you're still confused...
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    okay

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Firstly, I'm assuming that the file *format* you're using is your own, allowing you to modify it.

    The easier way is to simply use scanf and read formatted text input instead. That makes various assumptions about your character set, but since ASCII and UTF-8 are basically the standard across all modern platforms, that shouldn't be an issue.

    The harder way would be, for example, to read a 64-bit integer in the little endian format, then to store that in an unsigned long long. Indeed, it's likely that you can accomplish that with a simple call to fread since your implementation probably represents unsigned long longs in such a way, although it's trickier if you have an implementation where sizeof (unsigned long long) is greater than 8 or has padding bits, or it uses big-endian/middle-endian byte orders instead.

    Short answer: If you want to write portable code, either read up on data serialisation ("The Practice of Programming" discusses this fairly well) or use scanf. Otherwise, if you don't care that your code won't run on other systems/C implementations, then the following call is sufficient:

    unsigned long long int n;

    fread(&n, sizeof n, 1, stream);
    Sebastiani likes this.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I've been reading up on this and am still a little confused about what would happen if there were less than the array's number of bytes, do these get lost or can they be recovered?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,062
    Quote Originally Posted by Once-ler View Post
    I've been reading up on this and am still a little confused about what would happen if there were less than the array's number of bytes, do these get lost or can they be recovered?
    That is the reason that fread returns the number of bytes that were read. Suppose your file is 1234 bytes and you use a buffer of 1000 bytes. In this case, the first call to fread could possibly read 1000 bytes, the second call could possibly read 234 bytes and the third call would then read no bytes (this indicates either the end of the file or an error; you have to check which).

    I say "could possibly" because it is up to the operating system how it handles this. Maybe it will only give you at most 512 bytes at a time, or maybe it only gives you one byte at a time, who knows. Point is you have to check.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread records into a struct
    By Machiaveli in forum C Programming
    Replies: 1
    Last Post: 10-14-2006, 03:35 PM
  2. fread struct with unknown size
    By ReggieBE in forum C Programming
    Replies: 12
    Last Post: 03-27-2006, 11:19 AM
  3. nested struct , fread ,fwrite
    By magicz69 in forum C Programming
    Replies: 4
    Last Post: 08-01-2004, 09:47 AM
  4. struct / fread ?
    By optimus in forum C Programming
    Replies: 11
    Last Post: 04-03-2004, 01:16 AM
  5. fread is reading too much
    By CeeCee in forum C Programming
    Replies: 7
    Last Post: 02-18-2002, 02:44 PM

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