Thread: Buffered input

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    43

    Buffered input

    Hello everyone,
    I was trying this very simple piece of code on my Linux machine (running ubuntu 9.10)

    Code:
    #include <stdio.h>
    
    int main(void){
    	char c;
    	
    	while((c=getchar())!=EOF)
    	putchar(c);
    	
    	return 0;
    }
    When I run this program and input

    test<CTRL+D>

    i get the my input back

    testtest

    how is this possible ? The input is buffered, meaning that input should be available to the function after pressing enter ENTER button

    Also after running the program and by just pressing <CTRL+D> the program is exiting. Shouldn't it exit after pressing the enter button? Because it is after pressing the enter key the input will be read by the getchar() ?

    Thank you

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Did you consider that your output might be buffered too?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    I didn't get you.
    Can you please explain what is happening ?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What is happening is you are posting twice in the same forum for the same topic and sending private messages to get help. Impatient, are we?
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Oh you replied in the Linux thread. What do you mean my output is buffered?

    I added fflush(stdout) under putchar(c) and the results are the same.

    so the code is now

    Code:
    #include <stdio.h>
    
    int main(void){
    	int c;
    	
    	while((c=getchar())!=EOF){
    	putchar(c);
    	fflush(stdout);}
    	
    	printf("\nha\n");
    	return 0;
    }
    No i dont think it is buffered, because when i input for example 123 and i press enter it gets printed after pressing.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    Let me clarify what I said in my first post, and where my problem is

    Let me again explain to you what my problem is.

    When I input test<ENTER>, test gets printed on the other line

    Code:
    test
    test
    Continuing, inputing test<pressing CTRL+D> i get the same thing next to it

    Code:
    test<CTRL+D>test
    Pressing <CTRL+D>

    it exits the program.

    Shouldn't typing test<CTRL+D> do nothing ? Meaning it should wait for the ENTER button to be pressed to read the input ?

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    "CTRL+C" is a special character to send an interrupt to the program, usually causing it to exit. "CTRL+D" is another special character, read about it here Definition: end-of-transmission character or elsewhere. So pressing this key may be like the program getting an "EOF" which is why the program would exit.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have merged the two threads. Please do not repeat your threads again. There is no need to message people to ask them to look at your thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Yeah why are you hitting people up in private messages and cross-posting? This is not the way to get help. It seems folks are trying to help you and you are not accepting the help being offered...

    As for your question I am not getting what you don't understand about the code you wrote...you cannot figure out why the while() loop broke? You set the condition yourself by watching for an EOF character, then supplying it...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    43
    All right. My mistake.
    As for my question, i know pressing CTRL-D should signal EOF. But my question is, when I input a word, like test for exmple and press CTRL-D, it gets automatically outputed like

    (what happens in console, italic is the output)
    Code:
    $./test
    test<CTRL+D>test
    
    123<ENTER>
    123
    <CTRL+D>
    PROGRAM EXITS
    My question is, knowing that getchar() uses buffer, meaning, all what is input will be available to it after pressing ENTER, why is it that when i type test<CTRL+D> without pressing enter i get test as output and i can continue inputting... so what is CTRL+D doing here? What is wrong ?
    Thank you

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It means that if there is data already in the buffer, that buffer is considered "complete" without having to press return.

    Similarly, you might just get the buffer if you type in a lot of data without pressing return.

    ctrl-d returning EOF is only special when there is NO data in the buffer to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  3. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM