I guess something like this is more than what you want. It is not as difficult as it can be, but it is very simple and can be easily distributed to accommodate 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);
}
source
share