Thread: Writing a struct to a file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Writing a struct to a file

    HI,
    i have created a struct with x strings in it
    x - is a user choice int.

    now what im trying to do is writing 10 structs of this kind into a file, and opening it again later and read them from the file.

    a simple fwrite(&struct,sizeof(struct),1,file) wont work since the size varies..
    what can i do?

    thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It won't work IF the struct looks like this:
    Code:
    struct {
         char *ptr;
    }
    but if the code looks like this:
    Code:
    struct {
         char string[256];
    }
    It will be fine.

    I am presuming you are NOT trying to produce a readable text file, you just want a file to store information (that does not need to be read like text).

    If you want a readable file, then you cannot use this method in any case. You will have to take each field in the struct, convert it to a string (if it isn't already) and then write it out. In that case, you could just use one line per field and an empty line between structs or something.

    If you don't need a readable file and need to deal with a pointer to a string, again work per field, but don't bother to convert to a string, and write an int indicating the string length prior to the string.
    Code:
    struct {
         int ID;
         float num;
         char *data;
    }
    Write ID (sizeof(int)), write num (sizeof(float)), then write another int containing the length of data, then write data. When you read in, read in an int to ID, a float to num, read in another int indicating the length of data, then read that many bytes into data.

    You might want to look at this too:
    http://sourceforge.net/apps/mediawik...=Serialization
    Last edited by MK27; 04-12-2010 at 05:39 AM.
    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

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by MK27 View Post
    It won't work IF the struct looks like this:
    Code:
    struct {
         char *ptr;
    }
    but if the code looks like this:
    Code:
    struct {
         char string[256];
    }
    It will be fine.
    ok so i get it that
    Code:
    struct {
         char string[256];
    }
    will be fine, but my problem is that struct1 contains
    Code:
    struct {
      char string1[256];
      char string2[256];
      char string3[256];
    }
    and struct 2 contains:

    Code:
    struct {
      char string1[256];
      char string2[256];
    }


    if all ofthem had only one string it would of easier, but the number varies..
    how do i create\ write \ load a struct with varied amount of strings?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by urbanleg View Post
    if all ofthem had only one string it would of easier, but the number varies..
    how do i create\ write \ load a struct with varied amount of strings?
    A struct cannot have a varied number of strings. You are talking about two different kinds of structs. If you want to store those together, you would need some kind of separator indicating this, or an index.
    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

  5. #5
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    You can write all kinds of info about the data to a file before the actual data.
    Like a image file header etc.
    In your case you could first write two integers to the file.
    One for the number of "struct a's" written and one for the number of "struct b's" written.
    Then read the specified number of each.

    For that matter , you could write a byte indicating what data is next in line,,,
    if type a store accordingly, if type b ...

    You could write file header info telling where to find a particular block of data and fseek to that location!!!
    You can do all kinds of fancy stuff , you just have to read and write according to specs.
    Last edited by HowardL; 04-12-2010 at 02:14 PM.

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