Hi all,
I'm newbie to C and i'm trying do a simple program for adding records into a text file using file streams. I'm using gets() to get user name with spaces i dont know y i'm not about to use that gets() function properly.Could anyone tell what exactly the problem with this code... thanks in advance...
Code:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct employee
{
char name[150];
};
int main()
{
addRecord();
getchar();
return 0;
}
int addRecord()
{
FILE *fp;
struct employee emp[1000];
char ch;
int i=0,count=0;
fp = fopen("E:\\SRI\\C_Projects\\tel_Directory_struct\\intoFile.txt","w");
if(fp!= NULL)
{
printf("enter how many records u want to add??");
scanf("%d",&count);
for(i=0;i<count;i++)
{
printf("Enter the Name of the employee:");
gets(emp[i].name); // here problem encounters and not able to receive
// user input,program is terminating...:(
fprintf(fp,"%s\n",emp[i].name);
}
fclose(fp) ;
getchar();
return 0;
}
else
{
printf("File couln't open...");
exit(1);
}
}