Hi guys,
its me again i got problem on my file function.
A function new() that appends a new contact information into the file.
The function asks for a name, telephone no., and a cell phone no., stores these in a structure, then inserts it at the end of the file. If the file does not exist yet, create a new file.
I got this working, but in case of any corrections...here it is:
Code:
void new (void)
{
FILE *fp;
contact_rec X;
if (( fp = fopen ("phone.dat", "ab")) == NULL)
{
printf ("Error : Unable to open file");
exit(1);
}
else
{
printf("Enter a Name: "); fflush(stdin);
gets(X.name);
printf("Enter tel no.: "); fflush(stdin);
gets(X.tel);
printf("Enter cel no.: "); fflush(stdin);
gets(X.cel);
fwrite(&X, sizeof(contact_rec), 1, fp);
fclose(fp);
}
}
A function view() that displays all contact information from the file.
The function opens the file, reads one record at a time and displays it onscreen.
I get an error of "Expression Syntax"
Here is my code:
Code:
void view (void)
{
FILE *fp;
contact_rec X;
if ((fp = fopen ("phone.dat", "rb")) == NULL)
{
printf("Error : File is empty");
exit(1);
}
while (fread(&X, sizeof(contact_rec), 1, fp)) != NULL) <----"Expression Syntax Error"
{
printf("%s",X.name);
printf("%s",X.tel);
printf("%s",X.cel);
}
fclose(fp);
}
Last. A function search() that displays the contact information given the name.
The fucntion asks for a name, searches the name from the file, then displays the telephone no., and cell phone no. If the name is not found, display "record not found."
I havent got to this one yet since im having problem in the second function
Heres my code anyway:
Code:
void search (void)
{
FILE *fp;
contact_rec X[4];
char c;
int i = 0;
if ((fp = fopen ("phone.dat", "rb")) == NULL)
{
printf ("Error : file is does not exist");
exit(1);
}
printf ("Enter a name to be search: ");
gets(s);
for ( i = 0; i < 5; i++)
{
if ((strcmp (s, X[i].name)) == 0)
{
printf("%s", X[i].tel);
printf("%s", X[i].cel);
}
else
{
printf("Error : Record not found");
}
}
Your help is very much appreciated.