Save and Load Struct Array.

I tried to code a little C++ Programm to save and load 100 Struct variables. In School we begann to code with fstream, but i can't figure out how to do it. My Struct looks like this:

[code]
struct satz{
char[30] name;
char[30] eff1;
char[30] eff2;
char[30] eff3;
char[30] eff4;
};

satz zutat[100];
[/code]

It would be very nice if someone can write a Noob friendly tutorial. This isn't homework, its a little Programm for myself.

Comments

  • You can use stdio procedures:
    [code]typedef struct
    {
    char name[30];
    char eff1[30];
    char eff2[30];
    char eff3[30];
    char eff4[30];
    } satz;
    void main()
    {
    FILE *fk;

    satz zutat[100];
    int js;

    // Assign your array elements here

    //Write:
    fk=fopen("myfile.txt","w");
    for(js=0;js<100;js++)
    fprintf(fk,"%30s%30s%30s%30s%30s",zutat[js].name,zutat[js].eff1,zutat[js].eff2,zutat[js].eff3,zutat[js].eff4);
    fclose(fk);

    //Read:
    fk=fopen("myfile.txt","r");
    for(js=0;js<100;js++)
    fscanf(fk,"%30s%30s%30s%30s%30s",&(zutat[js].name),&(zutat[js].eff1),&(zutat[js].eff2),&(zutat[js].eff3),&(zutat[js].eff4));
    fclose(fk);
    }[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion