Struct File and array problem Please help
Anyone knows why this code isn't working. the problem seems to be related to the size of the char arrays.
I am trying to read some data into a struct, write the struct to a file, close the file, read the data again and print it.
when I read and print the data again, it displays a lot of garbage characters.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct clientInfo
{
char name[25];
char address[25];
char telNumber[25];
char attentionItem [25];
char problemDescription[5];
};
int main()
{
int i;
const int size = 2;
struct clientInfo data[size];
struct clientInfo dat;
FILE *clientFile;
clientFile = fopen("clients.txt","w");
for (i = 0; i < size; i++)
{
fflush(stdin);
printf ("Please enter your name\n");
gets (data[i].name);
fflush(stdin);
printf ("Please enter your address\n");
gets (data[i].address);
fflush(stdin);
printf ("Please enter your telephone number\n");
gets (data[i].telNumber);
fflush(stdin);
printf ("Please enter the item which aquires attention\n");
gets (data[i].attentionItem);
fflush(stdin);
printf ("Please enter a description of the problem\n");
gets (data[i].problemDescription);
fflush(stdin);
fflush(stdin);
fwrite(*data[i],sizeof(data[i]),1,clientFile);
}//endfor
fclose(clientFile);
clientFile = fopen("clients.txt","r");
while(!feof(clientFile))
{
fread(&dat,sizeof(clientInfo),1,clientFile);
printf("name is %s\n",dat.name);
printf("address is %s\n",dat.address);
printf("telephone is %s\n",dat.telNumber);
printf("attention is %s\n",dat.attentionItem);
printf("problem is %s\n",dat.problemDescription);
}//end while
fclose(clientFile);
system("pause");
return 0;
}//endmain