Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[50];
int roll;
char addrs [100];
};
main()
{
struct student stu;
FILE *fp;
int ch;
char c;
char sn[50];
printf("\n\t\t\t\tMENU:");
printf("\n1. Update Record in the file");
printf("\n2. Search by Name");
printf("\n3. Exit");
printf("\nEnter Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
fp=fopen("student.txt", "ab");
while (1)
{
printf("\nEnter Name: ");
scanf("%s", stu.name);
fflush(stdin);
printf("\nEnter Roll No.: ");
scanf("%d", &stu.roll);
fflush(stdin);
printf("\nEnter Address: ");
scanf("%s", stu.addrs);
fflush(stdin);
fwrite(&stu,sizeof(stu),1,fp);
printf("\nDo you want to continue?(Y/N): ");
scanf("%c", &c);
if(c=='n'||c=='N')
{
break;
}
}
fclose(fp);
break;
case 2:
fp=fopen("student.txt", "rb");
printf("\nEnter Name: ");
gets(sn);
while(fread(&stu,sizeof(stu),1,fp))
{
if(strcmp(sn,&stu.name)==0)
{
printf("\n\t%s \t %d \t %s", stu.name, stu.roll, stu.addrs);
getch();
break;
}
}
fclose(fp);
getch();
break;
case 3:
exit(1);
}
}
What do i want ?
1. I want to store Name with space for example - Seth Rollins
2. I want to store Address with house number (integer value) and string with space for example - 234, Texas
Will somebody help me to modify this code with fgets function and i also do not want to use fflush function as it is not portable but without this function my code doesn't work properly.