why buffered?

This is a discussion on why buffered? within the C Programming forums, part of the General Programming Boards category; I cannot check this program at the place where I am now. Could someone just help me with this ? ...

  1. #1
    Unregistered
    Guest

    why buffered?

    I cannot check this program at the place where I am now. Could someone just help me with this ?

    I just need to know whether each line entered by user is printed on the standard output right after it is entered.

    [code]
    #include <stdio.h>

    int main (void)
    {
    char input [1000];

    while (gets (input))
    puts (input);

    return 0;
    }
    [\code]

    (I know I shouldnīt use gets())

    So letīs say user enters these lines

    Will this program work like this:

    >first line
    first line
    >second line
    second line
    >third line
    third line

    or more like this...?

    >first line
    >second line
    >third line
    first line
    second line
    third line

    Thank you !

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    The first way
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It would work the same as your first example, but don't use gets, it is very unsafe because it does not check the size of your buffer and if the input is greater will write to memory that you do not own. Try fgets instead:
    Code:
    #include <stdio.h>
    
    int main (void)
    {
      char input [1000];
      
      while (fgets (input, 1000, stdin) != NULL)
        printf ("%s", input);
      
      return 0;
    }
    Code:
    Output:
    
    This is a test
    This is a test
    Another test
    Another test
    Last one
    Last one
    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    Thanks alot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parsing a buffered binary data
    By KianTern in forum C Programming
    Replies: 6
    Last Post: 05-23-2008, 10:58 AM
  2. exec and buffered output
    By optimus in forum C Programming
    Replies: 5
    Last Post: 04-26-2004, 04:25 AM
  3. Buffered I/O subsystem
    By bto19 in forum C Programming
    Replies: 2
    Last Post: 03-21-2004, 05:49 PM
  4. Buffered vs Unbuffered
    By PutoAmo in forum C Programming
    Replies: 4
    Last Post: 10-24-2002, 09:54 PM
  5. Buffered input
    By CeeCee in forum C Programming
    Replies: 12
    Last Post: 11-25-2001, 03:15 AM

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