can any1 tell me wat i am doing wrong?
when i search for a name which is in the record for updating, it says name not found. the only name i can update is the first one
?????
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "function.h"
#include "structure.h"
#include "screen.h"
/*************************External Global variables - in Mainmenu.c ***/
contactFileN[30];
extern FILE *fpC;
/************************************ For OPtion "Open Files" ***/
void OpenFiles ( void)
{
char fileTitle[30];
char reply;
printf ("\n\nOpen Files\n");
printf ("------------\n\n");
printf ("Enter the file name (without file extension) : ");
fflush (stdin);
gets (fileTitle);
strcpy ( contactFileN, fileTitle);
strcat ( contactFileN, ".1");
/********************************* Open Basic File ***/
fpC = fopen (contactFileN, "r+b");
if (fpC == NULL)
{
printf ("File open error on %s\n", contactFileN);
printf("Do you want to create new file (y/n) ?\n");
fflush(stdin);
scanf("%c", &reply);
reply=tolower(reply);
if (reply=='y')
{
fpC = fopen(contactFileN, "w+b");
printf("%s file creation successful\n", contactFileN);
}
else
{
printf ("Press <ENTER> to continue\n");
fflush(stdin); getchar();
return;
}
}
else
{
printf("File %s open successfully\n", contactFileN);
}
}//OpenFiles
/************************* Events for Add Records Option ***/
void AddRecords( void )
{
/* ?????????????????????????????????????????? */
struct contact y;
printf ("\n\nKey in the following information :\n");
//Infor for basic file
y.status = 1; //record active
printf ("Name : ");
fflush (stdin);
gets (y.name);
printf ("Key in an unique id code (numeral only) :");
scanf ("%ld", &y.id);
printf ("Birth dates (dd mm yyyy) : ");
scanf ("%d %d %d", &y.birth.dd, &y.birth.mm, &y.birth.yy);
printf ("Sex Code (M-male, F-Female) : ");
fflush (stdin);
y.sex= getchar();
//Infor for Contact file
printf("Enter status (1 for active, -1 for inactive) : ");
scanf("%d", &y.status);
printf("Address : \n");
printf("Enter Street : ");
fflush(stdin);
gets(y.addr1.street);
printf("Enter Postal Code : ");
scanf("%ld", &y.addr1.pCode);
printf("Enter Country : ");
fflush(stdin);
gets(y.addr1.country);
printf ("Key in Telephone no (Press -9 if none) : ");
scanf("%ld", &y.tel1);
printf("Key in Mobile Phone no (Press -9 if none) : ");
scanf("%ld", &y.mp1);
printf("Key in Pager no (Press -9 if none) : ");
scanf("%ld", &y.pg);
printf("Key in Email address : ");
fflush(stdin);
gets(y.email);
//Write to Basic File
fseek (fpC, 0L, SEEK_END);
fwrite (&y, sizeof(y), 1, fpC);
printf ("One records added to the following two files : %s\n",
contactFileN);
printf ("Press <ENTER> for next record\n");
fflush (stdin);
getchar();
}//AddRecords
void UpdateInfo(void)
{
struct contact y;
long ntel1;
long nmp1;
long npg;
char nemail[30];
char nstreet[51];
long npCode;
char ncountry[20];
int i=0;
int found=0;
char name[30];
char fileTitle[30];
printf ("Enter the basic file name (without file extension) : ");
fflush (stdin);
gets (fileTitle);
strcpy ( contactFileN, fileTitle);
strcat ( contactFileN, ".1");
/********************************* Open Basic File ***/
fpC = fopen (contactFileN, "r+b");
if (fpC==NULL)
{
printf("Error Opening File..press ENTER to continue\n");
fflush(stdin);
getchar();
return;
}
printf ("%s file open successful\n",contactFileN);
printf("Please enter your name : ");
fflush(stdin);
gets(name);
//rewind the pointer
rewind(fpC);
while (1)
{
//read the file
if (fread(&y, sizeof(y), 1, fpC)==0)
{
break;
}
rewind(fpC);
//search through the file
if (strcmpi(name, y.name)==0)
{
found=1;
break;
}
else
{
printf("Name not found. Press ENTER to continue.\n");
fflush(stdin);
getchar();
return;
}
i++;
}//while
if (found=1)
{
printf("Enter New Address : \n");
printf(" Enter new street : ");
fflush(stdin);
gets(nstreet);
printf(" Enter new postal code : ");
scanf("%ld", &npCode);
printf("Enter new country : ");
fflush(stdin);
gets(ncountry);
printf("Enter new Phone (-9 if none) : ");
scanf("%ld", &ntel1);
printf("Enter new Mobile (-9 if none) : ");
scanf("%ld", &nmp1);
printf("Enter new Pager : (-9 if none) : ");
scanf("%ld", &npg);
printf("Enter new Email : ");
fflush(stdin);
gets(nemail);
//position the pointer to current position
fseek(fpC, -(long)sizeof(y),SEEK_CUR);
strcpy(y.addr1.street, nstreet);
strcpy(y.email, nemail);
strcpy(y.addr1.country, ncountry);
y.mp1= nmp1;
y.pg=npg;
y.tel1=ntel1;
y.addr1.pCode=npCode;
//updates the file
fwrite(&y, sizeof(y), 1, fpC);
rewind(fpC);
printf("Congratulations! Record updated successfully!\n");
printf("\nPress ENTER to continue\n");
fflush(stdin);
getchar();
}
fclose(fpC);
} // UpdateInfo