Saving struct to file
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 st开发者_如何学Goruct
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?
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.
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);
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);
}
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:
- s11n library.
- This answer.
- Tpl library.
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.
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
精彩评论