0

I'm having a little trouble with the code below and I can not for the life of me figure out what went wrong and why it is displaying what it does, any help or assistance would be most appreciated. It is supposed to allow 5 lines of text to be entered and display those 5 lines onscreen, however it only allows 4 lines to be entered, and 4 are displayed. Please help!

#include <stdio.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,"wr");
if(fp==NULL)
{
    printf("unable to open File");
}
for(n=1;n<6;n++)
{
    printf("\nEnter 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=1;n<6;n++)
{
    fgets(string,100,fp);
    printf("%s",string);
}
fclose(fp); // close after reading.
return 0;
}
The10thDoctor
  • 125
  • 1
  • 13
  • First thing to do is use 0 based loops instead of 1 based (much more natural for C type languages - I note you already have an `n+1` that looks like it assumes a 0 based loop). Apart from that looks like it should work. Try printing `n` after the first loop to see that it got to 6... – John3136 Jun 20 '13 at 03:29
  • @John3136 He's not using any arrays, except a couple of strings that he doesn't index. – Barmar Jun 20 '13 at 03:31
  • What happens if you `fflush(stdout)` after each iteration? – n3rd4n1 Jun 20 '13 at 03:32
  • @n3rd4n1 Why is that necessary? He's not reading the file until after he closes it, which will flush everything. – Barmar Jun 20 '13 at 03:33
  • @Barmar: Notice I said `stdout` not the `fp` file. – n3rd4n1 Jun 20 '13 at 03:35

4 Answers4

2

The problem is that scanf("%s", filename); doesn't consume the newline after the filename. So your first call to gets() reads this newline as an empty line.

Add:

gets(string);

after that line to use up the rest of the line before you start reading input lines.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Here is the modified code. Added gets instead of scanf and added return 0; if file is not opened.

#include <stdio.h>

int main()
{
char string[100];
char filename[20];
int n=0;
FILE *fp;
printf(" Enter the name of file to open ");
gets(filename);
fp =fopen(filename,"wr");
if(fp==NULL)
{
    printf("unable to open File");
    return 0; // do not proceed
}
for(n=1;n<6;n++)
{
    printf("\nEnter line %d:",n);
    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");
    return 0; // do not proceed
}
for(n=1;n<6;n++)
{
    fgets(string,100,fp);
    printf("%s",string);
}
fclose(fp); // close after reading.
return 0;
}
raj raj
  • 1,932
  • 1
  • 14
  • 15
0

replace scanf("%s",filename) with gets(filename)

Chelsea Wang
  • 599
  • 2
  • 5
  • 19
0

To get rid of the newline in the buffer right after your call to scanf, you can simply add getchar();:

scanf("%s", filename); 
getchar();

But do adjust your for loops to start at 0, since you add 1 to n i.e:

for(n=0;n<6;n++)
      ^

After making those changes I was able to input 6 lines and then print all of them out.

As you've noticed, buffered input can be pesky if you don't deal with it properly as it can be inserted in your subsequent input calls. Don't be tempted to flush the stdin.

Here are some recommended alternatives on how to deal with this.

Community
  • 1
  • 1
Nobilis
  • 7,310
  • 1
  • 33
  • 67