Saving structure to file

I want to save a multidimensional array to a file. For example, the structure:

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++;
}

In this example, I want to save data to a file in this format (plain 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?

+3
source share
6 answers

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);

}
+2
source

Remember that saving the original structures to a file like this is not transferred at all. The compiler may add an addition to the structure (changing sizeof (your_struct)), endianness may be different, etc. If this, however, does not bother, then fwrite () works fine.

, - , , , .

+6

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;
}

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

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);

,  - http://c-faq.com/struct/io.html

fwrite(&somestruct, sizeof somestruct, 1, fp);
+3
+2

fileio, , .

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

, , .

+1

Besides the name of the object main, which may cause you to have any number of strange problems: just drag 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
+1
source

Source: https://habr.com/ru/post/1766620/


All Articles