I have used the following code. 's' is a variable of the structure. Can someone show me the currect usage of fseek as this code doesnt replaces but adds the edited portion in the file.
the file name 'g' is of the mode r+
HA
In the output section whenever the roll no matches it shows me 2 lines at a time and doesnt waits for the input.
Do you want to change the name? (y/n) Do you want to change the class? (y/n)
void edit()
{
int x;
char ch,c;
printf("\nEnter roll no. \n");
scanf("%d", &x);
rewind(g);
while(!feof(g))
{
fscanf(g, "%d %s %d %s %s", &s.roll, s.name, &s.standard, s.sub, s.address);
if(s.roll==x)
{
printf("\nDo you want to change the name? (y/n)\n");
scanf("%c",&ch);
if(ch=='y')
{
printf("\nEnter new name\n");
scanf("%s", s.name);
}
printf("\nDo you want to change the class? (y/n)\n");
scanf("%c",&ch);
if(ch=='y')
{
printf("\nEnter new class\n");
scanf("%d",&s.standard);
}
printf("\nDo you want to change the subject? (y/n)\n");
scanf("%c", &ch);
if(ch=='y')
{
printf("\nEnter new subject\n");
scanf("%s", s.sub);
}
printf("\nDo you want to change the address? (y/n)\n");
scanf("%c", &ch);
if(ch=='y')
{
printf("\nEnter new address\n");
scanf("%s", s.address);
}
fseek(g,sizeof(struct student) ,SEEK_SET);
fprintf(g,"%d %s %d %s %s",s.roll, s.name, s.standard, s.sub, s.address);
}
}
fclose(g);
}