Newbie Question: EOF in MS Visual Studio 2003?

Discussion in 'C Programming' started by entropy123, Jul 2, 2003.

  1. entropy123

    entropy123 Guest

    Hey all,

    A newbie, I am working through Kernighan and Ritchie.

    I'm having a problem with one of their test programs & MS VS 2003. In
    particular the EOF value for VS2003 in winxp is apparently '-1'
    However when I enter '-1' as a character in this program the program
    (which ought to terminate when EOF is input) will not quit. I need to
    hit 'ctrl-c' to exit the program ----> no output...

    Thanks for your help,
    entropy

    Here is the program:

    #include <stdio.h>

    int main()
    {
    int c, nl;

    nl = 0;
    while (( c = getchar() ) != EOF)
    if ( c == '\n')
    ++nl;
    printf("%d\n", nl);
    }
    entropy123, Jul 2, 2003
    #1
    1. Advertising

  2. entropy123 <> scribbled the following:
    > Hey all,


    > A newbie, I am working through Kernighan and Ritchie.


    > I'm having a problem with one of their test programs & MS VS 2003. In
    > particular the EOF value for VS2003 in winxp is apparently '-1'
    > However when I enter '-1' as a character in this program the program
    > (which ought to terminate when EOF is input) will not quit. I need to
    > hit 'ctrl-c' to exit the program ----> no output...


    > Thanks for your help,
    > entropy


    You have misunderstood what EOF is. It is *not* a character. It does
    not exist in the stream at all. It is an extra-special return code from
    getchar(). It means "due to some implementation-specific magic, the
    stream closed up. I'll pull this -1 thingy out of my hat to make up for
    it." Really. That's all it means.
    If you want to tell the program yourself that there will be no more
    data, you have to use some implementation-specific magic yourself. On
    Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D. On Amiga,
    it is ctrl-\. I don't know about other platforms.

    > Here is the program:


    > #include <stdio.h>


    > int main()
    > {
    > int c, nl;


    > nl = 0;
    > while (( c = getchar() ) != EOF)
    > if ( c == '\n')
    > ++nl;
    > printf("%d\n", nl);
    > }


    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Immanuel Kant but Genghis Khan."
    - The Official Graffitist's Handbook
    Joona I Palaste, Jul 2, 2003
    #2
    1. Advertising

  3. entropy123

    Lew Pitcher Guest

    Joona I Palaste wrote:
    > entropy123 <> scribbled the following:
    >
    >>Hey all,

    >
    >
    >>A newbie, I am working through Kernighan and Ritchie.

    >
    >
    >>I'm having a problem with one of their test programs & MS VS 2003. In
    >>particular the EOF value for VS2003 in winxp is apparently '-1'
    >>However when I enter '-1' as a character in this program the program
    >>(which ought to terminate when EOF is input) will not quit. I need to
    >>hit 'ctrl-c' to exit the program ----> no output...

    >
    >
    >>Thanks for your help,
    >>entropy

    >
    >
    > You have misunderstood what EOF is. It is *not* a character. It does
    > not exist in the stream at all. It is an extra-special return code from
    > getchar(). It means "due to some implementation-specific magic, the
    > stream closed up. I'll pull this -1 thingy out of my hat to make up for
    > it." Really. That's all it means.
    > If you want to tell the program yourself that there will be no more
    > data, you have to use some implementation-specific magic yourself. On
    > Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D.


    Or not ;-)

    /If/ the stream is redirected from a file, then the physical act of
    attempting to read beyond the last byte of data invokes the passing of EOF.

    OTOH, if the stream is read from a 'character special device' (i.e., a
    terminal or similar), the OS interprets (or not) the characters, and
    delivers the metadata signal of "reading beyond the last byte of data" based
    on the character read. Since this character is configurable, it /can/ be
    ctrl-D, but doesn't /have to be/ ^D. Indeed, if the device is conditioned to
    be deliver data in 'raw' mode (admittedly, a condition and operation that's
    off-topic in CLC), the system may /never/ signal that an end-of-data
    condition has been encountered.


    > On Amiga,
    > it is ctrl-\. I don't know about other platforms.

    [snip]


    --

    Lew Pitcher, IT Consultant, Application Architecture
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    Lew Pitcher, Jul 2, 2003
    #3
  4. entropy123

    entropy123 Guest

    >
    > You have misunderstood what EOF is. It is *not* a character. It does
    > not exist in the stream at all. It is an extra-special return code from
    > getchar(). It means "due to some implementation-specific magic, the
    > stream closed up. I'll pull this -1 thingy out of my hat to make up for
    > it." Really. That's all it means.
    > If you want to tell the program yourself that there will be no more
    > data, you have to use some implementation-specific magic yourself. On
    > Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D. On Amiga,
    > it is ctrl-\. I don't know about other platforms.
    >

    Joona,

    Thanks for your help. It certainly is ctrl-z. I can understand why a
    book last revised in 1989 might not have the info...but not in the VS
    2003 help files....

    entropy
    >
    > > Here is the program:

    >
    > > #include <stdio.h>

    >
    > > int main()
    > > {
    > > int c, nl;

    >
    > > nl = 0;
    > > while (( c = getchar() ) != EOF)
    > > if ( c == '\n')
    > > ++nl;
    > > printf("%d\n", nl);
    > > }
    entropy123, Jul 2, 2003
    #4
  5. entropy123

    istartedi Guest

    entropy123 <> wrote in message
    news:...
    > >
    > > You have misunderstood what EOF is. It is *not* a character. It does
    > > not exist in the stream at all. It is an extra-special return code from
    > > getchar(). It means "due to some implementation-specific magic, the
    > > stream closed up. I'll pull this -1 thingy out of my hat to make up for
    > > it." Really. That's all it means.
    > > If you want to tell the program yourself that there will be no more
    > > data, you have to use some implementation-specific magic yourself. On
    > > Windows, this involves typing ctrl-Z. On Unix, it is ctrl-D. On Amiga,
    > > it is ctrl-\. I don't know about other platforms.
    > >

    > Joona,
    >
    > Thanks for your help. It certainly is ctrl-z. I can understand why a
    > book last revised in 1989 might not have the info...but not in the VS
    > 2003 help files....


    I think they were assuming that the target audience was familiar with the
    DOS command-line. I'm not trying to insult you, but I think you need to
    learn more about the command-line. Google for some tutorials. It feels
    funny for me to say this, but I have to say don't waste your time learning
    too much about the DOS/Windows CLI. The *NIX shells seem to be where the
    future is at as far as CLIs are concerned, and you can run them on Windows
    (easiest way is via Cygwin) if you want to practice without partitioning
    your hard drive.

    Raise your hand if you think "knows all the ins and outs of the DOS/Windows
    CLI tools and batch file programming" is going to sound a lot like "COBOL
    programmer" in a few years. Actually, it already does to me.

    --$teve
    istartedi, Jul 3, 2003
    #5
  6. [OT] Re: Newbie Question: EOF in MS Visual Studio 2003?

    On Thu, 3 Jul 2003, istartedi wrote:
    >
    > Raise your hand if you think "knows all the ins and outs of the DOS/Windows
    > CLI tools and batch file programming" is going to sound a lot like "COBOL
    > programmer" in a few years. Actually, it already does to me.


    Batch-file programming is definitely old-skool in my mind, but I have to
    say that my life at the moment would be a lot more tedious *without* batch
    files. DOS command-line tools: well, I *was* going to say that I use
    them all the time, until I realized that most of what I use regularly is
    really the DJGPP textutils set, and not DOS at all - cat, less, diff,...
    I hardly ever use type, more, or fc anymore. And I assume you're not
    going to count del, deltree, dir, copy, mkdir, move, or ren. For a speedy
    typer, they're a heck of a lot simpler than the Windows file system
    interface. :)

    -Arthur
    Arthur J. O'Dwyer, Jul 3, 2003
    #6
  7. entropy123

    entropy123 Guest

    >
    > I think they were assuming that the target audience was familiar with the
    > DOS command-line. I'm not trying to insult you, but I think you need to
    > learn more about the command-line. Google for some tutorials. It feels
    > funny for me to say this, but I have to say don't waste your time learning
    > too much about the DOS/Windows CLI. The *NIX shells seem to be where the
    > future is at as far as CLIs are concerned, and you can run them on Windows
    > (easiest way is via Cygwin) if you want to practice without partitioning
    > your hard drive.
    >
    > Raise your hand if you think "knows all the ins and outs of the DOS/Windows
    > CLI tools and batch file programming" is going to sound a lot like "COBOL
    > programmer" in a few years. Actually, it already does to me.
    >
    > --$teve


    $teve,

    No, not insulted at all. I'm famiiar with *NIX shells and their
    command lines, but just had no clue in winxp. :) One of my many
    misconceptions had to do with "winxp EOF" searches that yielded little
    or poor results...

    I agree I need to look into explicit DOS command line commands....at
    least for the time being...

    entropy
    entropy123, Jul 3, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bootstrap Bill

    Visual Studio .net 2003 and Frotpage 2003

    Bootstrap Bill, Sep 12, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    594
    Bootstrap Bill
    Sep 17, 2004
  2. wh
    Replies:
    2
    Views:
    505
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  3. rockdale
    Replies:
    1
    Views:
    598
    Juan T. Llibre
    Aug 23, 2006
  4. xman
    Replies:
    0
    Views:
    481
  5. xman
    Replies:
    1
    Views:
    588
    Victor Bazarov
    Aug 17, 2005
Loading...

Share This Page