Thread: Yet another EOF issue

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    32

    Yet another EOF issue

    Hello,

    I tried the following code to experiment with EOF:


    Code:
        int c;
        
        while(1)
        {
            printf("<GET>");
            c = getchar();
            if (c == EOF) {
                printf("<EOF>");
            }
            printf("<PUT>");
            putchar(c);
        }
    When I ran the program and typed asd and pressed CTRL+D (EOF), I got the following output:

    <GET>asd<PUT>a<GET><PUT>s<GET><PUT>d<GET>

    No EOF value was passed to the program. So far so good, I understand. When I pressed CTRL+D (EOF) again with no text entered, I expected EOF to be passed to the program which happened but the <EOF> text wasn't printed - I tried setting a breakpoint on the printf line and it did trigger yet nothing was printed. Why?

    When I add the break command after the call to printf, the <EOF> text gets printed when I repeat the above procedure (run, type asd, press CTRL+D twice).

    I'm totally confused...

    EDIT: I'm using a *nix system, not Windows.
    Last edited by kkk; 09-01-2011 at 10:37 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Y'know what... you need to try that on a different system/computer ... it is possible you've found a bug in your linux shell.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Are you running this directly from a console, or within some IDE?

    Code:
    $ ./a.out 
    <GET>hello
    <PUT>h<GET><PUT>e<GET><PUT>l<GET><PUT>l<GET><PUT>o<GET><PUT>
    <GET><EOF>$ 
    $ 
    $ ./a.out 
    <GET>hello
    <PUT>h<GET><PUT>e<GET><PUT>l<GET><PUT>l<GET><PUT>o<GET><PUT>
    <GET>world<PUT>w<GET><PUT>o<GET><PUT>r<GET><PUT>l<GET><PUT>d<GET><EOF>$
    The first test, I pressed ctrl-d once, at the start of an input line.
    The second test I pressed ctrl-d after the 'd' of world, which caused the output of 'world'.
    A second ctrl-d then caused normal EOF to be detected.

    Is this other reply relevant?
    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.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Y'know what... you need to try that on a different system/computer ... it is possible you've found a bug in your linux shell.
    I tried running the code outside my IDE and it worked. So the problem is in the IDE!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    Y'know what... you need to try that on a different system/computer ... it is possible you've found a bug in your linux shell.
    Possible, yes, but highly, highly, highly unlikely. This is not some crazy program that tests the limits of the Linux terminal and shell, it likely revolves around kkk's misunderstanding of how the terminal and program handle the keyboard signals.

    @kkk: How do you know the program got an EOF if nothing was printed? I'd like to see a cut-and-paste of you running the program, from start to finish, not a paraphrase of what happened.

    EDIT: Nevermind, I see it was the IDE!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  2. Issue with FAQ example
    By 3saul in forum C Programming
    Replies: 2
    Last Post: 04-16-2007, 05:13 AM
  3. PHP/IIS Issue
    By ChadJohnson in forum Tech Board
    Replies: 7
    Last Post: 09-25-2005, 11:02 AM
  4. pop up issue
    By RoD in forum Tech Board
    Replies: 6
    Last Post: 07-09-2003, 05:46 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM