The majority of the program works. Only issue I'm having is when prompted to enter text into the file the loop start like this:
Enter line 1:Enter line 2:
--after I enter a text then the rest of the loop works as intended
Enter line 3:
Enter line 4:
Enter line 5:
And only 4 lines of text is printed out
What do I need to change to get Enter line 1:Enter line 2: show as 2 separate lines my loop looks right not sure what I'm doing wrong.
Code:
#include <stdio.h>
#include <conio.h>
int main()
{
char string[100];
char filename[20];
int n=0;
FILE *fp;
printf(" Enter the name of file to open ");
scanf("%s",filename);
fp =fopen(filename,"w+");
if(fp==NULL)
{
printf("unable to open File");
}
for(n=0;n<5;n++)
{
printf("Enter line %d :",n+1);
gets(string);
fputs(string,fp);
fputs("\n",fp);
}
fclose(fp); /*close the file*/
fp =fopen(filename,"r");
if(fp==NULL)
{
printf("unable to open File");
}
for(n=0;n<5;n++)
{
fgets(string,100,fp);
printf("%s",string);
}
fclose(fp); // close after reading.
return 0;
}