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

This is a discussion on How can I read and write a structure to and from a file? within the C Programming forums, part of the General Programming Boards category; I'm trying to write a structure to a file, and read that structure in again, possibly with different padding, byte ...

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

    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,046
    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,006
    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,659
    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));
    }



    ITSA
    Socket Library!

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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

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