Iam is trying to create a program that can save the indicated information in a file. So I'am is able to load it and can use it later.
Here my strands are two structures:
typedef struct {char name [11]; Char Euremann [11]; }person; Typedf strait {int nebuferson; Person * person list; } PersonGOD; Personal Control Controller; Int likes; To load \ n "" (3) \ n "" (3) load controller \ n "" to start the amount of x {printf ("(1)); Scanf ("% d (Option); Switch (Preferred) {Case 1: Initial Part (and Controller); Breakage; Case 3: Save Persons tofile (& Controller); Breakage; Case 5: Load Controller (and Controller); }} While (flag);
Load, save, start the function here:
zero initializePersons (personGOD * controller) {int amount, i; Printf ("How many people will be made"); Fscanf (stdin, (I = 0; i & lt; zodiac; i ++) for "calligraphy" (amount, size (person)); Printf ("name:"); Fscanf (stdin, "% s", controller-> personlist [i] .name); Printf ("last name:"); Fscanf (stdin, "% s", controller-> interest [I] .efternamn); // eftername = lastname} controller-> numbofpersons = amount; // update how many people we have) SavePersonsToFile zero (personGOD * controller) {FILE * fileptr; if ((fileptr = Fopen ("C: \\ user \\ x \\ desktop \\ openstrat \\ individuals \", "R + B")) == faucet () {f Printf (stderr, "Error opening file:% s \ N", strerror (errno)); Exit (0); } If (Eighlight (controller, size (person GOD), 1, file repritor) 1) FPrintf (stadder, "% s \ n", strrayer (wrong)); Fclose (fileptr); } Zero Load Controller (PersonalGod * Controller) {FILE * fileptr; If ((fileptr = fopen ("C: \\ user \\ x \\ desktop \\ openstrat \\ individuals \", "R + B")) == faucet () {fprintf (stderr, "file opening error:% (FRED (Controller, size (person.god), 1, file repritor) 1) {FPrint F (steder, "% s \ n", serre (Wrong));} Fclose (fileptr);}
What will happen when I save-> close-program-> open-> load is that the numofpersons are correct and in the person list and now Will not indicate the allocation of the crash .
How to complete it with a tough time.
How to save and restore data indicated
You usually do not want to place raw pointer data in the file, because if you later run your program again (Or on another machine, use other compilers, etc.), there is no guarantee that the objects will have the same address.
Car, the field will write separately, because this time you want to write what kind of data you have no idea. This is the first parameter of ezine to type const void *
.
An example is indicated with zero (assuming the error):
fwrite (& amp; controller-> number-head, size (controller-> gt; ; Numerical), 1, filetype); Filitt (Admin-> Individual List, Size (* Controller-> Personal List), Controller- & gt; Number Display, FilePrint);
You use the same logic to read the file:
fread (& amp; admin-> numeric, size (controller-> numeric 1) File repritor); Controller- & gt; Personlist = Colok (Controller-> Adoption-> Adjustment-> Volume (* Controller-> Personal List); Fred (Controller-> Individual List, Size (* Controller-> Personal List), Controller- & gt; FilePrint);
This solution has problems with endlessness, so it is not completely portable, but this is another story.
No comments:
Post a Comment