Good afternoon, I have a structure

struct bird{ string name; string type; string habitat; bool migrant; bird *next; }; 

I need to write it to a file, and then extract it. I write like this (write):

 bird bTemp; ofstream output_file("Bird", ios::trunc); else { while(fTemp != NULL) { output_file.write((char*)&bTemp, sizeof(bird)); } } 


 void ReadListFromFile() { bird bTemp; ifstream input_file("Bird"); if (! {*)&bTemp, sizeof(bird)); } } 

As a result, a binary file is created, but a segmentation error occurs when trying to read

  • I apologize for the text format, just learning the basics of the local editor - Yegor Sokolov

4 answers 4

And what did you actually expect to receive? After all, you have instances of the string classes, and even the next pointer. You think nominally .. but you have to think about those bytes that you write there and about those bytes that you read and then try to use as if they were always in memory.

  • Actually, yes, did not take into account, okay, I will do it differently - Yegor Sokolov

I advise you to be more attentive, since in the second after you very accurately noticed that you are trying to work with the notebook data as if they are always in memory. Ie you first need to change the data structure to:

  struct bird{ string name; string type; string habitat; bool migrant; }; + struct BIRD{ bird label; bird *next; }; 

You should write data to the file and not links to it (bird * next). that is, by clicking on the link you write only the data structures (bird label), and when you read them then using a cycle you will reassemble them into the structure (struct BIRD). I think that by doing this, you have not had any problems with recording data not with their extraction!

    As I have noticed, type casting should look like this: *)&bTemp, sizeof(bird)); 

    Yes, in fact, in the first case too:

     output_file.write((char *)&bTemp, sizeof(bird)); 

    Because it is not a char, but a pointer to a char, which is necessary for the write and read methods.

    • Everything is the same when trying to output - a segmentation error, in the debugger it is clear that nothing is written to bTemp - Egor Sokolov
     #include <stdio.h> void main() { struct sss { char fam[20]; int oklad; } s1; FILE *f1; // запись в файл f1 = fopen("1.bin","wb"); while (true) { puts(rus("Введите фамилию или 0 (конец ввода)")); scanf("%s",&s1.fam); if (note.fam[0] == '0') break; puts(rus("Введите оклад")); scanf("%d",&s1.oklad); fwrite(&s1,sizeof(s1),1,f1); } fclose(f1); puts(rus("Файл 1.bin записан\n")); // чтение из файла f1 = fopen("1.bin","r+b"); while(true) { fread(&s1,sizeof(s1),1,f1); if(!feof(f1)) break; printf("fam = %s, oklad = %d",s1.fam,s1.oklad); } fclose(f1); 
    • Well, so you pile do everything will be the norm. - Sema
    • Sema, thanks of course, but I wanted to do it through ifstream, ofstream. But as cy6erGn0m correctly noted, you need to follow what you write to the file. I had to change the prog a bit and everything was earned. In any case, thanks for the example - Egor Sokolov