简体   繁体   中英

Putting struct array into file and reading it

I want to put a struct array into a file and also put an integer in the same file (I want to use fwrite() for the array). The problem seems to occur when I try to read it. I am new to C, so maybe you could explain to me how it works. Thanks in advance.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Data{

    char street[40];
    char building[10];
    char email[30];
    unsigned long long number;

}Data;

int main(){

    Data data[3];
    Data output[3];
    int size = 2;
    int sizeout;
    // putting something inside
    strcpy(data[0].building, "11");
    strcpy(data[0].email, "a@a.a");
    data[0].number = 37068678102;
    strcpy(data[0].street, "Street1");

    strcpy(data[1].building, "21");
    strcpy(data[1].email, "b@b.b");
    data[1].number = 37068678432;
    strcpy(data[1].street, "Street2");

    //writing into file (I want to use "wb")
    FILE *write;
    write = fopen("temp.bin","wb");
    //if I understand correctly, fprintf is the way to put in an integer
    fprintf(write,"%d",size);
    //putting array in
    fwrite(data,sizeof(Data),2,write);
    fclose(write);



    FILE *read;
    fseek(read, 0, SEEK_SET);

    read = fopen("temp.bin","rb");
    //gettinf the int out
    fscanf(read,"%d",&sizeout);
    //getting array out
    fread(output,sizeof(Data),2,read);
    fclose(read);

    //printing what I got
    for(int i = 0; i < sizeout; ++i){
        printf("First struct: %s %s %s %llu\n", output[i].building, output[i].email, output[i].street, output[i].number);
    }
    return 0;
}

This is your code but a little cleaner. what this guy says is true

You call fseek on read pointer before fopen .

But that is also not necessary because when you open a file in r the pointer will always be in the first position regardless

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#define doc "doc.bin"

typedef struct Data{

    char street[40];
    char building[10];
    char email[30];
    unsigned long long number;

}Data;

int sizeofdata =  sizeof(Data);


int main(){



FILE *f;

f = fopen(doc, "wb"); //if you want to open it in w remember that all that is contained with in will be erased , i recomend a+b

      Data data[3];
    Data output[3];
    int size = 2;
    int sizeout;
    // putting something inside
    strcpy(data[0].building, "11");
    strcpy(data[0].email, "a@a.a");
    data[0].number = 37068678102;
    strcpy(data[0].street, "Street1");

     fwrite(&data, sizeofdata,1, f);//the one represents the quantitie of regiesteres you wanna imput , you can make it a variable

     fclose(f);


     if ((f=fopen(doc,"rb"))!=NULL){//this makes for a cleaner solution it will be simpeler in the long run
            while (!feof(f)) {
            fread(&output, sizeofdata,1, f);
            if (!feof(f)){
               printf("First struct: %s %s %s %llu\n", output[0].building, output[0].email, output[0].street, output[0].number);
            }
}
     }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM