Monday, March 22, 2010

Accepts input from keyboard and writes to file using c++

/* Files : Formatted Disk I/O Functions */
/* fscanf() fprintf() */
/* Accepts input from keyboard and writes to file */

#include
#include

void main()
{
FILE *fp;
char another='Y';
char name[40];
int age;
float sal;
fp=fopen("EMP.TMP","w");
if(fp==NULL)
{
printf("ERROR : Unable to open File");
return;
}

while(another=='Y')
{
printf("\nEnter Name, Age and Salary\n");
scanf("%s %d %f",name,&age,&sal);
fprintf(fp,"%s %d %f",name,&age,&sal);

printf("\n\nDo you want to continue ? (Y/N) ");
fflush(stdin);
another=getche();
}
fclose(fp);

}