I have written the following C code to write five names to a text file and print the same on the monitor. While reading the data from the file, the last string is printed twice on the screen..But why..
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1, *fp2, *fp3;
char name[10];
int n,c;
clrscr();
printf("How many names..\n");
scanf("%d", &n);
fp1 = fopen("source.txt", "w");
while( n > 0 )
{
fflush(stdin);
printf("Enter the name\n");
gets(name);
fprintf(fp1,"%s\n", name);
n--;
}
fclose(fp1);
fp1 = fopen("source.txt", "r");
printf(" You entered the following names\n");
while( !feof(fp1) )
{
fscanf(fp1, "%s", name);
printf("%s\t", name);
}
getch();
}