Thread: Writing struct to file

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    Writing struct to file

    I has read that when writing a whole struct to a file with fwrite() is bad, as it's nonportable.

    But does it change anything to write the whole struct with fputc() ?
    Something like this:
    Code:
    char *writePointer = &structVariable;
    for(int i=0; i < STRUCT_SIZE; i++) {
      fputc(writePointer[i], outputBuffer);
    }
    Won't this make just as much portability issues as the fwrite?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Drogin View Post
    I has read that when writing a whole struct to a file with fwrite() is bad, as it's nonportable.
    As long as you use "wb", I am sure fwrite() is fine for this on linux and windows and almost certainly Mac, so I'm not sure where "unportable" comes in.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    As long as you use "wb", I am sure fwrite() is fine for this on linux and windows and almost certainly Mac, so I'm not sure where "unportable" comes in.
    Because compilers always and everywhere use the same padding.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    Because compilers always and everywhere use the same padding.
    Wouldn't that (potential incompatibility) only apply if the file itself was passed from one executable to another, separately (& differently) compiled executable?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    Wouldn't that (potential incompatibility) only apply if the file itself was passed from one executable to another, separately (& differently) compiled executable?
    It certainly applies to that situation.

    Here fputc() will walk along the struct one byte at a time, ignoring the alignment requirements of the various object types leading to portablility problems not only between different executables but also within the same executable.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by itCbitC View Post
    Here fputc() will walk along the struct one byte at a time, ignoring the alignment requirements of the various object types leading to portablility problems not only between different executables but also within the same executable.
    Isn't there a way to explicitly specify how the padding should be handled in the struct definition?

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Subsonics View Post
    Isn't there a way to explicitly specify how the padding should be handled in the struct definition?
    You mean by a preprocessor directive like pragma?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Wouldn't that (potential incompatibility) only apply if the file itself was passed from one executable to another, separately (& differently) compiled executable?
    Well, I suppose. But what else do you think non-portable means in this context? The code is certainly portable, in the sense that it will work on basically any machine. It just won't do the same thing on any of them.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by itCbitC View Post
    You mean by a preprocessor directive like pragma?
    I saw that GCC had a -fpack-struct flag and that there is a pack(1) alternative as well. I was curious if this could perhaps be combined with making the padding by hand using bitfields. Not sure if that would work or if all compilers support this either.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Subsonics View Post
    I saw that GCC had a -fpack-struct flag and that there is a pack(1) alternative as well. I was curious if this could perhaps be combined with making the padding by hand using bitfields. Not sure if that would work or if all compilers support this either.
    Yep! you can certainly change the default alignment of the various object types so that they are packed together side-by-side, but all of this implies non-portable. Other compilers may not support the fpack-struct switch or the fact that some compilers use pragma pack 1 instead of the gcc variant pragma pack(1). Bottom line is that altering the natural alignment of the different object types produces code that is not portable and degrades performance.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM