Reputation: 13
I am new to C in linux. I am trying to store data to file and read them back. Is this the correct way. When I try to compile this i am getting errors. Can anyone help me please. thanks in advance.
#include<stdio.h>
typedef struct
{
int select;
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
} addressbook;
addressbook a[5];
FILE *fp;
int main()
{
int i;
for( i=0; i<5 ; i++)
{
printf("enter details\n");
printf("enter lastname:\n");
scanf("%s", a[i].lastname);
printf("enter firstname:\n");
scanf("%s", a[i].firstname);
printf("enter address:\n");
scanf("%s", a[i].address);
printf("enter phone number:\n");
scanf("%s", a[i].phonenumber);
fp = fopen("addressbook.dat","a+");
fwrite(&a, sizeof(a), 1, fp);
fclose(fp);
}
for(i=0; i<5; i++)
{
fopen("addressbook.dat", "r");
fread(&a, sizeof(a), 1, fp );
printf("lastname:%s\n", a[i].lastname);
printf("firstname:%s\n", a[i].firstname);
printf("address:%s\n", a[i].address);
printf("phonenumber:%s\n", a[i].phonenumber);
fclose(fp);
}
return 0;
}
i am not getting any output. it was blank.
Upvotes: 0
Views: 10428
Reputation: 2860
Check out this code, and let me explain you what all was wrong in your code.
#include<stdio.h>
typedef struct
{
int select;
char lastname[25];
char firstname[25];
char address[25];
char phonenumber[25];
} addressbook;
#define ARRAYLEN 2
addressbook a[ARRAYLEN];
FILE *fp;
int main()
{
int i;
fp = fopen("addressbook.dat","a+");
for( i=0; i<ARRAYLEN ; i++)
{
printf("enter details\n");
printf("enter lastname:\n");
scanf("%s", a[i].lastname);
printf("enter firstname:\n");
scanf("%s", a[i].firstname);
printf("enter address:\n");
scanf("%s", a[i].address);
printf("enter phone number:\n");
scanf("%s", a[i].phonenumber);
fwrite(&a[i], sizeof(a), 1, fp); /* notice, array indexed */
}
fclose(fp);
fopen("addressbook.dat", "r");
for(i=0; i<ARRAYLEN; i++)
{
fread(&a[i], sizeof(a), 1, fp );
printf("lastname:%s\n", a[i].lastname);
printf("firstname:%s\n", a[i].firstname);
printf("address:%s\n", a[i].address);
printf("phonenumber:%s\n", a[i].phonenumber);
}
fclose(fp);
return 0;
}
Actually, your code as-is (apart from the edit you've already done) isn't so incorrect, but it had some small yet crucial flaws.
The only real change is this :-
fwrite(&a[i],...
and,
fread(&a[i],...
i.e. pass the address of the particular array-element that you want to write, not the entire array. Also, even though you were passing address of the entire array, the no. of byte/characters you were asking library to write, was just sizeof(thestructure), so essentially the remaining was truncated. Without that, what you were writing into the file was something like...
A <-- file contents after, first iteration
AAB <-- file contents after, second iteration
AABABC <-- file contents after, third iteration
AABABCABCD <-- file contents after, fourth iteration
....
I think you'd figure out from that, what was wrong. Also the contents of your addressbook.dat was text, so a simple "cat addressbook.dat" (on Linux) would have told you what was wrong :-)
You are opening and closing file in every iteration. Now this is not an error, but just a sub-optimal thing, and quite likely to be something you do not want to do. File operations are costly, and opening/closing those cost quite a few CPU cycles. You are better off, opening file once for all writes, and once for reads. (Of course, once can remove the fclose() done after write-block and fopen() done before read-block as well, by just getting the file-pointer to the beginning of file -- left as an exercise for you).
While testing, no one wants to enter so much data. So I've added a #define (and with a newer compiler you can replace it with a const definition as well), that defines a macro which holds the addressbook array size. For testing, I keep it at "2". For production you can just change that value to "1000" and it will still work. Again, this wasn't an error, just a better style, if you will.
Oh, and BTW, pls get your indentation right. Are you coming from Python world ? Or it could be an artifact of the indentation required by SO for posting code-blocks.
HTH
Upvotes: 1
Reputation: 33391
It seems to me that you are not including the required headers. For example, printf
requires stdio.h
, so at the beginning for your file, you will need
#include <stdio.h>
CPlusPlus.com provides pretty good and easy to search documentation for C, so if you wish to use a function, you can look it up and find out which header is required.
Upvotes: 1