CTRL-C for EOF in Visual C++

This is a discussion on CTRL-C for EOF in Visual C++ within the C Programming forums, part of the General Programming Boards category; Say I have the following: Code: while ((ch = getchar()) != EOF) putchar(ch); and hit F5 to run it in ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    CTRL-C for EOF in Visual C++

    Say I have the following:

    Code:
    while ((ch = getchar()) != EOF)
           putchar(ch);
    and hit F5 to run it in Visual C++, if I force an EOF with CTRL-C it raises an exception. However if I run the program from the command line, it exits cleanly (although printing the ^C)

    Why is this? Say I wanted to print a message after the EOF is reached, it won't get to that part in Visual C++ debug mode. I just get a "first chance exception" message.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    EOF isn't Ctrl-C. Ctrl-Z, however, is.

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Oops, so it is. But another question:

    Say I have this:

    Code:
    int main(void)
    {
        int ch;
        int ct = 0;
        
        while ((ch = getchar()) != EOF)
            ct++;
        printf("%d characters read\n", ct);
        
        return 0;
    }
    Shouldn't CTRL-Z force the EOF at any time, even before a newline has been sent? Or does nothing happen until the newline flushes the buffer?

    Yet when I run this and type a few characters on one line, ending with a CTRL-Z and a newline, the program prints the ^Z then goes onto the new line and continues. Only then, if I type another ^Z and Enter will the program break out of the loop. I thought I understood what was going on with getchar() and EOF but it appears I was wrong!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    The common approach seems to be that you can signal EOF by pressing ctrl-z just once at the beginning of a line, and by pressing ctrl-z twice at any other point on the line.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by Salem View Post
    The common approach seems to be that you can signal EOF by pressing ctrl-z just once at the beginning of a line, and by pressing ctrl-z twice at any other point on the line.
    Not happening for me in Windows unfortunately. The only CRTL-Z that works is one at the start of the line. The two CTRL-Z's don't work in the middle of the line for me. I can however, CTRL-Z as the first character and it will end, which means I was basically talking nonsense about having to flush the buffer first.
    Last edited by Sharke; 02-22-2009 at 11:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

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