Thread: How to terminate program when EOF or pressing CRTL-D(linux)?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    How to terminate program when EOF or pressing CRTL-D(linux)?

    The problem form http://acm.uva.es/p/v102/10252.html
    How to terminate it when EOF or pressing CRTL-D?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    65
    If you think about it, what you enter can't be -1, because ``-1'' is two characters, and getchar is reading one character at a time. It turns out that the value of EOF as seen within your C program has essentially nothing to do with the keystroke combination you might use to signal end-of-file from the keyboard. EOF is essentially a signal to your program that no more characters will be available from that input, for whatever reason (end of a disk file, user is done typing, network stream has closed, I/O error, etc.).

    Depending on your operating system, you indicate end-of-file from the keyboard using various keystroke combinations, usually either control-D or control-Z. The operating system and stdio library then arrange that your C program receive the EOF value. (Note, however, that there are various translations involved along the way. Under normal circumstances, you should not explicitly check for the control-D or control-Z value yourself, nor will you find that the <stdio.h> macro EOF is defined to be either of these values.)

    (source [link] http://c-faq.com/stdio/eofval.html [/link )
    You rant and rave about it, but at the end of the day, it doesn't matter if people use it as long as you don't see.
    People are free to read the arguments, but if the only way for you to discover gravity is by jumping off a cliff, then that is what you're going to have to experience for yourself.
    Eventually, this "fast and loose" approach of yours will bite you one too many times, then you'll figure out the correct way to do things. - Salem

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    thank KoG.
    If I want to do this
    Code:
    input
    -------------------
    This is testing text.
    I will press CTRL-D to end of input. [CTRL-D]
    How do I read it?

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Ctrl+D sends EOF to the program. so test for EOF

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    while ( fgets( buff, sizeof buff, stdin ) != NULL )
    This exits when you enter the EOF sequence.

    As does this
    while ( (ch=getchar()) != EOF )
    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.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    ctrl-d is normally defined by stty program on login as meaning EOF - so the effect of particular keystroke(s) is defined on a per-user level.

    A lot of Linux/UNIX command line utilities honor EOF - ones like bc for example.

    Anyway, unless you are guaranteed that ctrl-d does generate EOF, don't put something in your menu that says "enter ctrl-D to exit".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. eof error in program
    By jamez in forum C Programming
    Replies: 2
    Last Post: 11-14-2005, 07:32 AM
  2. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  3. Replies: 2
    Last Post: 11-10-2003, 09:12 PM
  4. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM