Thread: How can I read and write a structure to and from a file?

  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057

    How can I read and write a structure to and from a file?

    I'm trying to write a structure to a file, and read that structure in again, possibly with different padding, byte alignment, edianness, and platforms.

    fread() and fwrite() don't work when writing the whole structure. Would they work when writing each member of the structure separately?

    How could I write a short to a file? Would this work? Would it work only for unsigned shorts?
    Code:
    putc(s/256, fp);
    putc(s%256, fp);
    Can every possible value of a short be written in that way? eg, even ones that would write a '\n' to the file?

    Is fwrite()ing a char okay? Even with different edians?
    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Endian only applies to multi-byte values. Since a char is a single byte, it has no endian.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So the best way to write a short to a file would be something like this?
    Code:
    fwrite(s/256, 1, 1, fp);
    fwrite(s%256, 1, 1, fp);
    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I would go one of two routes:
    a) store the structure as a text file.
    b) pack the structure with #pragma's, read each member in individually, and pass to a (conditionally compiled) byte reversal function.

    Code:
    void
    convert_to_little_endian(void * ptr, size_t size)
    {
    #ifdef BIG_ENDIAN_MACHINE
    	char 
    		temp, 
    		* begin = (char*)ptr, 
    		* end = begin + size;
    		
    	while(begin < --end)
    	{
    		temp = *end;
    		*end = *begin;
    		*begin++ = temp;
    	}
    #endif
    }
    
    int
    main(void)
    {
    	short 
    		value;
    		
    	convert_to_little_endian(&value, sizeof(short));
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Thanks, everyone. I'll see what happens.
    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