How can I save the structure in a file .... C lang

I would like to save the structure in a file. I would like to implement a function that does this work. I tried this code but it did not work.

struct utilisateur // enregestrement pour sauvegarder les details de l utilisateur { char nom[20]; char prenom[20]; int place; char depart[20]; char arrive[20]; char sexe; int nwagon; }; struct utilisateur utilis; struct utilisateur *Table[48]; void crea_fich(struct utilisateur *Tutilis) // creation un fichier, vous introduiez le nom, et le sotcker par enreg { FILE *f; if (f!==0) { printf("error in the name of file \n"); exit(1); } if (f=fopen(futilis,"w")==Null){ fprint("We can't creat file \n"); exit(1); } else{ f=fopen("futilis.dat","wb"); fwrite(Tutilis ,sizeof(utilisateur),1,f); } } 
+6
c pointers file
source share
6 answers

Not. You must write out your users individually, one at a time. You should not just blindly copy the memory representation of your structure to the file output buffer (which is clear what you are trying to do). Writing files in this way will lead to file intolerance (they will not be readable, except on the platform on which they were written) due to content and platform-specific structural elements.

+6
source share

Try this, and then if it does not do what you want, try to explain how it differs from what you want.

 void crea_fich(struct utilisateur *Tutilis) { FILE *f; size_t nwritten; f = fopen("futilis.dat","wb"); if (f == NULL) { fprintf(stderr, "Cannot open file for writing.\n"); exit(1); } nwritten = fwrite(Tutilis, sizeof Tutilis[0], 1, f); fclose(f); if (nwritten < 1) { fprintf(stderr, "Writing to file failed.\n"); exit(1); } } 
+6
source share

Michael C is right; using fwrite with a structure is wildly intolerable. But let me assume that you donโ€™t care, and you want something easy to write that will work.

The problem with your code is that you violated the golden rule of sizeof : never use sizeof with a type name . Instead, you should use sizeof with an lvalue and almost always with the dereference of another argument. Thus,

 Tutilis = malloc (sizeof (*Tutilis)); ... fwrite(Tutilis, sizeof (*Tutilis), 1, f); 

If you follow this recipe, it is much more difficult if you are mistaken.

+6
source share

A simple example :)

 // your struct struct Data { int first; double second; char third[10]; }; 

Then write struct!

 struct Data data = {22, 4.0, "Hi"}; FILE* output; output = fopen("Data.dat", "wb"); fwrite(&data, sizeof(data), 1, output); fclose(output); 

Finally, read the data from the file you created!

 struct Data data; FILE* input; input = fopen("Data.dat", "rb"); fread(&data, sizeof(data), 1, input); // you got the data from the file! fclose(input); 

Binary files are nightmares unless they are written and read wisely. You should pay a lot of attention to the architecture in which the file was created and where it will be read. The endiance and size of the variables are the most important. Also, if you have pointers inside your struct , the pointer that will be written to the file is not the actual data that the pointer points to. Sorry, I did not edit your code because it is filled with compilation errors :)

I deleted my first answer because it was so wrong, sorry :)

+5
source share

I understand that this is an old post, but it occurs when people look for this information, so I will give my own solution.

This is basically what I used in one of my games. I actually have some additional special library code in my own dialog function, but that is essentially it. I write data individually. I donโ€™t think I have a structure for this data, but there is no difference, just write your individual structure elements separately. As already mentioned, itโ€™s better.

 // returns 1 if successful, 0 if not int savemap(const char *map_name) { FILE *file = NULL; // open the file in write binary mode file = fopen(map_name, "wb"); // always check return values to see if it was opened okay if(file == NULL) { fprintf(stderr, "Error opening file for writing.\n"); return 0; } // write file ID, version and pills fwrite(MAP_ID, sizeof(char), strlen(MAP_ID)+1, file); fwrite(&MAP_VER, sizeof(unsigned char), 1, file); fwrite(&pills, sizeof(unsigned short), 1, file); // write the map data (unsigned int map[315]) fwrite(&map, sizeof(unsigned int), 315, file); // never forget to close the file fclose(file); return 1; } 
+1
source share

I first looked at character names like NULL and fprint , not to mention weird \0 . As an additional thought, you should close the file after writing to make sure it futilis red on disk, and double-check that the futilis variable is char* , which contains a valid write path.

0
source share

All Articles