Like Tree1Likes
  • 1 Post By grumpy

Why is my loop printing out 2 lines at one shot?

This is a discussion on Why is my loop printing out 2 lines at one shot? within the C Programming forums, part of the General Programming Boards category; The majority of the program works. Only issue I'm having is when prompted to enter text into the file the ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    29

    Why is my loop printing out 2 lines at one shot?

    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;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,245
    scanf("%s") stops when it encounters whitespace, but leaves in the stream waiting to be read. If that first whitespace character is a newline, the first gets() in your code will encounter it and return immediately.

    The general guideline to prevent what you are seeing is not to mix styles of input on the one stream. Use formatted input (aka scanf()) OR line-oriented input (gets(), fgets(), etc) OR character-oriented input (getc() fgetc(), etc). Don't mix and match them on any stream.


    Also: NEVER NEVER use gets(). It has been removed from the latest C standard for a reason - namely that it will happily write write past the end end the supplied string, and there is NOTHING the programmer can do to prevent that. Use fgets() instead. If you have a book that uses gets(), or that encourages you to use gets(), burn that book.
    jim mcnamara likes this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jul 2013
    Posts
    29
    You are right I replaced the first scanf with fgets and it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  2. Printing an array in lines of 10.
    By furiousferret in forum C++ Programming
    Replies: 10
    Last Post: 11-16-2004, 06:30 PM
  3. Printing 20 lines at a time
    By csmatheng in forum C Programming
    Replies: 5
    Last Post: 04-30-2002, 04:11 PM
  4. Printing multi-lines?
    By SyntaxBubble in forum Windows Programming
    Replies: 4
    Last Post: 11-27-2001, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21