i am writing a code to modify the information in a file. the code snippet for the same is:
Code:
/************************MODIFY THE RECORD************************************/
void modify(void)
{
char another= 'y';
char empname[40];
FILE *fp;
fp= fopen("details.txt", "a");
if(fp==0)
{
printf("cannot open file");
exit(0);
}
while(another=='y')
{
printf("\nEnter the name to be modified: ");
scanf("%s", empname);
rewind(fp);
while(fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
{
if(strcmp(emp.name, empname)==0)
{
printf("Enter new name: ");
gets(emp.name);
printf("Enter new age: ");
scanf("%d", &emp.age);
printf("Enter new salary: ");
scanf("%f", &emp.salary);
fseek(fp, -sizeof(struct _emp), SEEK_CUR);
fprintf(fp, "%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
}
}
printf("no match found\n");
printf("\nMOdify another record (y/n): ");
fflush(stdin);
another= getche();
}
fclose(fp);
return;
}
this code is skipping the while loop. even if i am entering the name that exists in the file details.txt, it skips the while loop and prints no match found.
the full code of the program if required is given below
Code:
#include<stdio.h>
#include<stdlib.h>
struct _emp
{
char name[40];
int age;
float salary;
};
struct _emp emp;
void add(void); //adds details to the file
void display(void); //display the details of the file
void init_file(void);//initializes the file
int menu_select(void);//displays the menu and ask for the users' choice
void modify(void); //modifies the record
int main(void)
{
char choice;
init_file(); //initialize the file
for(;;)
{
system("cls");
choice= menu_select();
switch(choice)
{
case '1': add(); break;
case '2': display(); break;
case '3': modify(); break;
case '4': exit(0);
default: printf("enter the correct choice\n"); getch(); break;
}
}
return 0;
}
/****************************INITIALIZE FILE**********************************/
void init_file(void)
{
FILE *fp;
fp= fopen("details.txt", "r");
if(fp==NULL)
{
fp= fopen("details.txt", "w");
if(fp==NULL)
{
printf("cannot open the file\n");
getch();
exit(0);
}
fprintf(fp, "Name\t\t\t\tAge\tSalary\n");
}
fclose(fp);
}
/*********************DISPLAYS MENU AND ASKS FOR CHOICE************************/
int menu_select(void)
{
system("cls");
char choice;
printf("\t\t\t\tMENU.\n");
printf("\t\t\t\t=====\n");
printf("\t\t1. Enter the details into the file\n");
printf("\t\t2. Display the details on the screen\n");
printf("\t\t3. Modify\n");
printf("\t\t4. Exit\n");
printf("Enter the choice: ");
choice= getche();
return(choice);
}
/********************************ADD DETAILS TO THE FILE**********************/
void add(void)
{
FILE *fp;
char ch= 'y';
fp= fopen("details.txt", "a");
if(fp==NULL)
{
printf("cannot open file");
getch();
exit(0);
}
while(ch=='y')
{
printf("\nEnter name: ");
scanf("%s", emp.name);
printf("\nEnter age: ");
scanf("%d", &emp.age);
printf("\nEnter salary: ");
scanf("%f", &emp.salary);
fprintf(fp, "%s\t\t\t\t%d\t%f\t\n", emp.name, emp.age, emp.salary);
printf("\nEnter your choice(y/n): ");
fflush(stdin);
ch= getche();
}
fclose(fp);
return;
}
/**************************DISPLAYS THE DETAILS*******************************/
void display(void)
{
FILE *fp;
fp= fopen("details.txt", "r");
if(fp==NULL)
{
printf("cannot open file");
getch();
exit(0);
}
fseek(fp, 20, SEEK_SET);
printf("\n");
printf("Name\t\t\tAge\tSalary");
while (fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
printf ( "\n%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary ) ;
getch();
fclose(fp);
return;
}
/************************MODIFY THE RECORD************************************/
void modify(void)
{
char another= 'y';
char empname[40];
FILE *fp;
fp= fopen("details.txt", "a");
if(fp==0)
{
printf("cannot open file");
exit(0);
}
while(another=='y')
{
printf("\nEnter the name to be modified: ");
scanf("%s", empname);
rewind(fp);
while(fscanf(fp, "%s %d %f", emp.name, &emp.age, &emp.salary)!= EOF)
{
if(strcmp(emp.name, empname)==0)
{
printf("Enter new name: ");
gets(emp.name);
printf("Enter new age: ");
scanf("%d", &emp.age);
printf("Enter new salary: ");
scanf("%f", &emp.salary);
fseek(fp, -sizeof(struct _emp), SEEK_CUR);
fprintf(fp, "%s\t\t\t%d\t%f", emp.name, emp.age, emp.salary);
}
}
printf("no match found\n");
printf("\nMOdify another record (y/n): ");
fflush(stdin);
another= getche();
}
fclose(fp);
return;
}