0

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, "[email protected]");
    data[0].number = 37068678102;
    strcpy(data[0].street, "Street1");

    strcpy(data[1].building, "21");
    strcpy(data[1].email, "[email protected]");
    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;
}
2
  • 2
    You call fseek on read pointer before fopen. Commented Dec 3, 2020 at 21:30
  • @user3121023 if I use fwrite for integer it crashes, as for data[0] it was an oversight, thanks anyway :) Commented Dec 3, 2020 at 21:41

1 Answer 1

1

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 rthe 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, "[email protected]");
    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);
            }
}
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.