c - how to store and read data from files -
I'm new to C in Linux. I am trying to store data and read them back. Is this the right way? When I try to compile it, I am getting errors. Can anyone help me please. thank you in advanced.
contains # lt; Stdio.h & gt; Select Typedef Structure {int; Four last names [25]; Four first names [25]; Four addresses [25]; Four phone numbers [25]; } Address book; Address Book A [5]; File * FP; Int main () {int i; For (i = 0; i <5; i ++) {printf ("Enter details \ n"); Printf ("Enter last name: \ n"); Scanf ("% s", a [i] .lastname); Printf ("Enter first name: \ n"); Scanf ("% s", a [i]. First name); Printf ("Enter address: \ n"); Scanf ("% s", a [i] .address); Printf ("Enter the phone number: \ n"); Scanf ("% s", a [i] .phonenumber); Fp = fopen ("addressbook.dat", "a +"); Fillit (& amp; a, sizeof (A), 1, FP); Fclose (fp); } (I = 0; i <5; i ++) {FOPN ("AddressbookDate", "R"); Fred (& amp; A, Size (A), 1, FP); Printf ("Last name:% s \ n", a [i] .lastname); Printf ("First name:% s \ n", a [i]. First name); Printf ("address:% s \ n", a [i] .address); Printf ("Phononumber:% s \ n", a [i] .phonenumber); Fclose (fp); } Return 0; } I can not find any output. It was empty.
Look at this code, and tell me what all was wrong with your code.
contains # lt; Stdio.h & gt; Select Typedef Structure {int; Four last names [25]; Four first names [25]; Four addresses [25]; Four phone numbers [25]; } Address book; #ArrayLEL2 Addressbook A [Arraylean]; File * FP; Int main () {int i; Fp = fopen ("addressbook.dat", "a +"); For (i = 0; i Actually, your code is similar (except for the edits you already have) is not so wrong, but there were some minor but important flaws in it.
-
The only real change is: -
filitt (and a [i], ... ... and,
fread (& a [i], ... either pass the address of that particular array element Whatever you want to write, not the whole array. In addition, even though you were giving the address of the entire array, the number of bytes / characters you were asking for the library was only the size (structure), so essentially the remaining It was hacked. Without it, would you write in the file There was something like this ...
A & lt; - After the file content, first run the AAB & lt; - the contents of the file, the second running AABABC & lt; - file I think you can tell about what was wrong. Besides, adding your content was Ressbook.dat text, so a simple "cat addressbook.dat" (on Linux) What was wrong: -)
-
Every visit that you are opening and closing the file is no longer an error, but only a sub-optimal There is a possibility, and there is a possibility of something that you do not want to do. File handling is expensive, and opening / closing those costs is a lot of CPU cycles, you are better, open a file at the time of writing all, and read it once. (Of course, once the file-pointer is at the beginning of the file - except for a drill for you) write-block and FOPN () can remove folkos () before the read-block.
-
While testing, no one wants to enter so much data, so I've added a # definition (and with a new compiler you can define it with the definition of a con Can also change), which defines a macro that has the address book array size, for testing, I place it on "2" for the output you can change that value to "1000" and it still works Will do Again, this was not an error, just a better style, if you do.
-
Oh, and BTW, pls get your indentation right Are you coming from the Python world? Or an indication of the indentation required by SO to post the code codes can be an artifact.
HH
Comments
Post a Comment