I want to save a multidimensional array in a file. For example a straight:
struct StructSub {unsigned short id; }; Straight MyStruct {struct StructSub sub [3]; }; // structure structure use the mainstream; Int i = 0; Whereas (i & lt; 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 is the easiest way to do this?
I'm guessing something that you want more. This is probably not in short form, but it is very straightforward and can easily be extended to accommodate other structures.
Zero instrument indent (file * file, indent) {int i = 0; While (i & lt; indent) {fprintf (file, "\ t"); ++ i; }} Zero WriteStructSub zero (file * file, StructSub * s, four * id, ind 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"); } Zero WriteMyStruct (file * file, MyStruct * s, char * id, int indent) {WriteIndent (file, indent); Fprintf (file, "MyStread% s {\ n", ID); Int i = 0; While (i and lt; 3) {four names [7]; Sprintf (name, "sub [% i]", i); WriteStructSub (file, & amp; ;; sub [i], name, indent + 1); ++ i; } Write indent (file, indent); Fprintf (file, "} \ n"); } Int main (int argc, char ** argv) {MyStruct s; Int i = 0; While (i & lt; 3) {s.sub [i] .id = i; ++ i; } FILE * Output = FOPAN ("Data Out", "W"); WriteMyStruct (Output, & S, "Main", 0); Fclose (production); }
Comments
Post a Comment