tags:

views:

140

answers:

6

I want to save a multidimensional array to a file. A struct for example:

struct StructSub {
    unsigned short id;
};
struct MyStruct {
    struct StructSub sub[3];
};

// Use the struct
struct MyStruct main;
int i = 0;
while (i < 3) {
    main.sub[i].id = i;
    i++;
}

For this example I want to save the data to a file in this format (normal text):

MyStruct main {
    StructSub sub[0] {
        id = 0;
    }
    StructSub sub[1] {
        id = 1;
    }
    StructSub sub[2] {
        id = 2;
    }
}

What's the easiest way to do this?

+1  A: 

you can use basic fileio, just make sure you write in binary.

FILE * pFile;
pFile = fopen( "structs.bin","wb" );
if ( pFile!=NULL ) {
  frwite( main, 1, sizeof(struct MyStruct), pFile );
  fclose (pFile);
}

If you do it this way though, it's not the most platform portable as there is endianness to consider.

jay.lee
+1 for mentioning endian issues.
Graeme Perrow
+2  A: 

Try this

struct StructSub {
    unsigned short id;
};
struct MyStruct {
    struct StructSub sub[10];
};

// Use the struct
struct MyStruct main;
int i = 0;
while (i < 10) {
    main.sub[i].id = i;
}

Write to file

FILE* output;
output = fopen("Data.dat", "wb");
fwrite(&main, sizeof(main), 1, output);
fclose(output);

Read file

struct Data data;
FILE* input;
input = fopen("Data.dat", "rb");
fread(&main, sizeof(main), 1, input);
// you got the data from the file!
fclose(input);

these link support what above code is all about - http://c-faq.com/struct/io.html

fwrite(&somestruct, sizeof somestruct, 1, fp);
JapanPro
Are you seriously suggesting a structure instance called `main` in a **`C`** program?!
JUST MY correct OPINION
ya, coz he want to save as file and it works
JapanPro
This definitely won't output in the file format that the question asks for.
Charles Bailey
Thanks, but that doesn't save the data like in my output example as Charles said.
Midas
this code is to illustrate the idea of saving file and retrieving it, you can adjust code as per requirement.
JapanPro
I want to save the struct to a normal text file. I don't know how to adjust this code, that's my question.
Midas
down voting? without checking wow
JapanPro
http://c-faq.com/struct/io.html (fwrite()
JapanPro
This code comes in handy now! Thanks.
Midas
but some people downvoted it without testing, dont know why?
JapanPro
+1  A: 

You can use Serialization libraries available that does this.

If you can use C++, there is Boost::Serialization library just for that. You may also like to checkout:

  1. s11n library.
  2. This answer.
  3. Tpl library.
KMan
I don't want to use library's.
Midas
+2  A: 

Do remember that saving raw structs to a file like this is not portable at all. The compiler might add padding to the struct (changing sizeof(your_struct)), endianness might be different, etc. If this is however, of no concern, then fwrite() works fine.

Remember that if your struct contains any pointers, you want to write the data that the pointer points to, not the value of the pointer itself.

Maister
+1  A: 

Aside from the name of the object being main, which may cause you any number of strange problems: just brute-force it -- there is no better way :)

/* pseudo code */
write struct header
foreach element
    write element header
    write element value(s)
    write element footer
endfor
write struct footer
pmg
+1  A: 

I'm guessing something like this is more what you want. It's not as terse as it could be, but it's very straightforward and can be easily extended to accomodate other structures.

void WriteIndent(FILE* file, int indent) {
    int i = 0;
    while (i < indent) {
        fprintf(file, "\t");
        ++i;
    }
}


void WriteStructSub(FILE* file, StructSub* s, char* id, int indent) {

    WriteIndent(file, indent);
    fprintf(file, "StructSub %s {\n", id);

    WriteIndent(file, indent + 1);
    fprintf(file, "id = %i;\n", s->id);

    WriteIndent(file, indent);
    fprintf(file, "}\n");

}


void WriteMyStruct(FILE* file, MyStruct* s, char* id, int indent) {

    WriteIndent(file, indent);
    fprintf(file, "MyStruct %s {\n", id);
    int i = 0;

    while (i < 3) {

        char name[7];
        sprintf(name, "sub[%i]", i);

        WriteStructSub(file, &s->sub[i], name, indent + 1);
        ++i;

    }

    WriteIndent(file, indent);
    fprintf(file, "}\n");

}


int main(int argc, char** argv) {

    MyStruct s;
    int i = 0;
    while (i < 3) {
        s.sub[i].id = i;
        ++i;
    }

    FILE* output = fopen("data.out", "w");
    WriteMyStruct(output, &s, "main", 0);
    fclose(output);

}
Jon Purdy
Thanks, that helped!
Midas