EOF question

Discussion in 'C Programming' started by newby2c, Aug 10, 2004.

  1. newby2c

    newby2c Guest

    Greetings all,

    Here is another incredibly silly question. I feel foolish asking it, but
    i've no other choice. I was going through K&R2 and at section 1.5.2
    (Character Counting), I decided to try out the program:

    /* count characters in input; 1st version */

    #include <stdio.h>

    int main(void)
    {
    long nc;

    nc=0;
    while(getchar()!=EOF)
    ++nc;
    printf("%ld\n",nc);
    return 0;
    }

    after compiling it, I ran it. I typed in some letters and numbers and they
    were repeated on the screen (as expected). But then what? How do I get the
    count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
    (program terminates), and ctrl-c (program terminates). What am I doing wrong
    or what am I understanding wrong? Any help is much appreciated.

    Best,
    newby2c
    --
    Tech Support: "How may I help you?"
    Customer: "I'm writing my first email."
    Tech Support: "Ok, what seems to be the problem?"
    Customer: "Well I can get the 'a'. But how do I put the circle around it?"
     
    newby2c, Aug 10, 2004
    #1
    1. Advertising

  2. newby2c

    newby2c Guest

    [ snip ]

    Sorry, I forgot to mention that I using a win98 box (if that even matters).

    newby2c
     
    newby2c, Aug 10, 2004
    #2
    1. Advertising

  3. newby2c

    Guest

    Have u tried the input -1 ??????

    newby2c wrote:
    >
    > [ snip ]
    >
    > Sorry, I forgot to mention that I using a win98 box (if that even matters).
    >
    > newby2c
     
    , Aug 10, 2004
    #3
  4. newby2c

    pete Guest

    newby2c wrote:
    >
    > [ snip ]
    >
    > Sorry, I forgot to mention that I using a win98 box
    > (if that even matters).


    Start your program, type your line,
    then hit the enter key,
    then your EOF, which is ctrl-z on my machine,
    then hit the enter key again.
     
    pete, Aug 10, 2004
    #4
  5. newby2c

    Richard Bos Guest

    wrote:

    [ Do not top-post. Corrected.

    > newby2c wrote:
    >
    > > Sorry, I forgot to mention that I using a win98 box (if that even matters).


    > Have u tried the input -1 ??????


    May I ask whatever gave you the idea that this could be useful? All it
    does is return, subsequently, '-', '1', ' ', and six times '?' (rather
    excessive, IYAM). Since all of these are in the required execution
    character set, this input is in fact guaranteed _not_ to end the loop.

    What the OP need to do (as usual, *sigh*), is to RTFFAQ. This question
    is, unfortunately, not currently in the web version, so I can't link to
    it (Steve: I think it should be, this is _very_ frequent), but it _is_
    in the version which is posted here regularly; it's question 12.1b.

    Richard
     
    Richard Bos, Aug 10, 2004
    #5
  6. "newby2c" <> writes:
    [...]
    > /* count characters in input; 1st version */
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > long nc;
    >
    > nc=0;
    > while(getchar()!=EOF)
    > ++nc;
    > printf("%ld\n",nc);
    > return 0;
    > }
    >
    > after compiling it, I ran it. I typed in some letters and numbers and they
    > were repeated on the screen (as expected). But then what? How do I get the
    > count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
    > (program terminates), and ctrl-c (program terminates). What am I doing wrong
    > or what am I understanding wrong? Any help is much appreciated.


    The program should read input characters until it hits an EOF
    condition. How that EOF condition is triggered is system-specific.

    You mentioned elsethread that you're running on a Windows system. You
    should be able to trigger and EOF by entering control-Z on a line by
    itself. The program should then print the number of characters
    entered and terminate. (Perhaps you didn't notice the number?) You
    should also be able to redirect the program's input from a text file;
    EOF will then be triggered when the program reaches the end of the
    file. Other systems will behave differently (which makes this
    paragraph mildly off-topic).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 10, 2004
    #6
  7. writes:
    > newby2c wrote:
    > >
    > > [ snip ]
    > >
    > > Sorry, I forgot to mention that I using a win98 box (if that even matters).
    > >
    > > newby2c

    >
    > Have u tried the input -1 ??????


    Please don't top-post. Your response should follow any quoted text,
    not precede it.

    I'm afraid you suggestion doesn't make any sense. EOF (which is
    typically equal to -1, but it doesn't have to be) is the value
    returned by getchar() when it reaches the end of the input. It's not
    a character value. Even if it were, entering "-1" would cause
    getchar() to return '-' followed by '1'.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Aug 10, 2004
    #7
  8. newby2c wrote on 10/08/04 :
    > /* count characters in input; 1st version */
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > long nc;
    >
    > nc=0;
    > while(getchar()!=EOF)
    > ++nc;
    > printf("%ld\n",nc);
    > return 0;
    > }
    >
    > after compiling it, I ran it. I typed in some letters and numbers and they
    > were repeated on the screen (as expected). But then what? How do I get the
    > count of the characters typed? I tried ctrl-d (I get a diamond),


    This would be OK on a Unixoid machine.

    > ctrl-z
    > (program terminates),


    This is OK on a DOS-Windows machine. You are probably using Dev-C++,
    and the Dos-box close instantly. Just add a 'pause' before the return
    of main():

    getchar();

    > and ctrl-c (program terminates).


    Don't do that.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 10, 2004
    #8
  9. newby2c

    CBFalconer Guest

    newby2c wrote:
    >

    .... snip ...
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > long nc;
    >
    > nc=0;
    > while(getchar()!=EOF)
    > ++nc;
    > printf("%ld\n",nc);
    > return 0;
    > }


    You get no special prizes for eliding blanks. The following is
    the same program, but legible:

    #include <stdio.h>

    int main(void)
    {
    long nc;

    nc = 0;
    while (getchar() != EOF)
    ++nc;
    printf("%ld\n", nc);
    return 0;
    }

    Admittedly, there are very few added blanks, but get in the habit.
    >
    > after compiling it, I ran it. I typed in some letters and numbers
    > and they were repeated on the screen (as expected). But then what?
    > How do I get the count of the characters typed? I tried ctrl-d (I
    > get a diamond), ctrl-z (program terminates), and ctrl-c (program
    > terminates). What am I doing wrong or what am I understanding
    > wrong? Any help is much appreciated.


    The ctrl-z caused the system to generate EOF, execute the printf,
    and terminate. The fact that you are confused makes me suspect
    you are using some form of IDE, rather than a command line
    compiler. The IDE is closing the window when (after) the program
    exits.

    Compile to disk (see your compiler systems documentation) and then
    run the resultant program. Or, better, learn to use the command
    line for compilation and running. This usually means (on windows
    systems) using a dosbox. Some IDEs have a way to show the user
    window after the program exits, Borlands among them.

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "A man who is right every time is not likely to do very much."
    - Francis Crick, co-discover of DNA
     
    CBFalconer, Aug 10, 2004
    #9
  10. newby2c

    newby2c Guest

    "Richard Bos" <> wrote in message
    news:...
    > What the OP need to do (as usual, *sigh*), is to RTFFAQ. This question
    > is, unfortunately, not currently in the web version, so I can't link to
    > it (Steve: I think it should be, this is _very_ frequent), but it _is_
    > in the version which is posted here regularly; it's question 12.1b.
    >
    > Richard


    Thank you. I will look for it when it is posted again.

    newby2c
     
    newby2c, Aug 10, 2004
    #10
  11. newby2c

    newby2c Guest

    "pete" <> wrote in message
    news:...
    > Start your program, type your line,
    > then hit the enter key,
    > then your EOF, which is ctrl-z on my machine,
    > then hit the enter key again.


    Thanks. I tried that. Here is what happens:

    I run the program.
    I type in some words and use spaces and tabs.
    I hit enter key.
    Caret starts at new line.
    I then hit ctrl-z.
    Nothing happens!
    I hit ctrl-z again and the program terminates, but the DOS box remains open.

    newby2c
     
    newby2c, Aug 10, 2004
    #11
  12. newby2c

    newby2c Guest

    "Keith Thompson" <> wrote in message
    news:...
    > The program should read input characters until it hits an EOF
    > condition. How that EOF condition is triggered is system-specific.
    >
    > You mentioned elsethread that you're running on a Windows system. You
    > should be able to trigger and EOF by entering control-Z on a line by
    > itself. The program should then print the number of characters
    > entered and terminate. (Perhaps you didn't notice the number?) You
    > should also be able to redirect the program's input from a text file;
    > EOF will then be triggered when the program reaches the end of the
    > file. Other systems will behave differently (which makes this
    > paragraph mildly off-topic).


    Thank you. But even ctrl-z on it's own line does not work. The first time I
    enter ctrl-z, nothing happens. If I do ctrl-z a second time, the program
    terminates with the DOS box remaining open.

    newby2c
     
    newby2c, Aug 10, 2004
    #12
  13. newby2c

    pete Guest

    CBFalconer wrote:
    >
    > newby2c wrote:


    > You get no special prizes for eliding blanks. The following is
    > the same program, but legible:


    > while (getchar() != EOF)
    > ++nc;


    I always use a compound statement in loops:

    while (getchar() != EOF) {
    ++nc;
    }
     
    pete, Aug 10, 2004
    #13
  14. newby2c

    newby2c Guest

    "Emmanuel Delahaye" <> wrote in message
    news:...
    [ snip ]
    > This is OK on a DOS-Windows machine. You are probably using Dev-C++,
    > and the Dos-box close instantly. Just add a 'pause' before the return
    > of main():
    >
    > getchar();
    >
    > > and ctrl-c (program terminates).

    >
    > Don't do that.


    Thanks for the suggestion. Yes, I am using Dev-C++. However I do not use the
    IDE to run my programs. I run everything from the command line in a DOS
    window. Unfortunately, your suggestion did not work. I get the same results
    as mentioned in my previously replies to others in this thread.

    newby2c
     
    newby2c, Aug 10, 2004
    #14
  15. newby2c

    newby2c Guest

    "CBFalconer" <> wrote in message
    news:...
    > You get no special prizes for eliding blanks. The following is
    > the same program, but legible:
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > long nc;
    >
    > nc = 0;
    > while (getchar() != EOF)
    > ++nc;
    > printf("%ld\n", nc);
    > return 0;
    > }
    >
    > Admittedly, there are very few added blanks, but get in the habit.


    Thanks for the advice.

    > The ctrl-z caused the system to generate EOF, execute the printf,
    > and terminate. The fact that you are confused makes me suspect
    > you are using some form of IDE, rather than a command line
    > compiler. The IDE is closing the window when (after) the program
    > exits.


    No. I do indeed use the command line from a DOS window to run programs.

    > Compile to disk (see your compiler systems documentation) and then
    > run the resultant program. Or, better, learn to use the command
    > line for compilation and running. This usually means (on windows
    > systems) using a dosbox. Some IDEs have a way to show the user
    > window after the program exits, Borlands among them.


    See above.

    newby2c
     
    newby2c, Aug 10, 2004
    #15
  16. newby2c wrote on 10/08/04 :
    > "Emmanuel Delahaye" <> wrote in message
    > news:...
    > [ snip ]
    >> This is OK on a DOS-Windows machine. You are probably using Dev-C++,
    >> and the Dos-box close instantly. Just add a 'pause' before the return
    >> of main():
    >>
    >> getchar();


    > I am using Dev-C++. However I do not use the
    > IDE to run my programs. I run everything from the command line in a DOS
    > window. Unfortunately, your suggestion did not work. I get the same results
    > as mentioned in my previously replies to others in this thread.


    I think you must hit <ctrl-z> <enter>

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
     
    Emmanuel Delahaye, Aug 10, 2004
    #16
  17. newby2c

    CBFalconer Guest

    newby2c wrote:
    >

    .... snip ...
    >
    > Thank you. But even ctrl-z on it's own line does not work. The
    > first time I enter ctrl-z, nothing happens. If I do ctrl-z a
    > second time, the program terminates with the DOS box remaining
    > open.


    That sounds like a fault in the system, probably Microsofts. Try
    ctrl-z return.

    --
    "Churchill and Bush can both be considered wartime leaders, just
    as Secretariat and Mr Ed were both horses." - James Rhodes.
    "A man who is right every time is not likely to do very much."
    - Francis Crick, co-discover of DNA
     
    CBFalconer, Aug 10, 2004
    #17
  18. On Mon, 9 Aug 2004 22:44:49 -0400, "newby2c" <>
    wrote:

    >Greetings all,
    >
    >Here is another incredibly silly question. I feel foolish asking it, but
    >i've no other choice. I was going through K&R2 and at section 1.5.2
    >(Character Counting), I decided to try out the program:
    >
    >/* count characters in input; 1st version */
    >
    >#include <stdio.h>
    >
    >int main(void)
    >{
    > long nc;
    >
    > nc=0;
    > while(getchar()!=EOF)
    > ++nc;
    > printf("%ld\n",nc);
    > return 0;
    >}
    >
    >after compiling it, I ran it. I typed in some letters and numbers and they
    >were repeated on the screen (as expected). But then what? How do I get the
    >count of the characters typed? I tried ctrl-d (I get a diamond), ctrl-z
    >(program terminates), and ctrl-c (program terminates). What am I doing wrong
    >or what am I understanding wrong? Any help is much appreciated.
    >
    >Best,
    >newby2c

    Here in windows xp <ctrl-z>==EOF so if I push some chars <ctrl-z> and
    '\n' the prog print the correnct number of char (less the last '\n').
    I think you could resolve your problem if you write in the code
    printf("%ld\n\n", nc); /* note \n\n */
    and use <ctrl-z> for end.
     
    RoSsIaCrIiLoIA, Aug 10, 2004
    #18
  19. newby2c

    newby2c Guest

    "CBFalconer" <> wrote in message
    news:...
    > That sounds like a fault in the system, probably Microsofts. Try
    > ctrl-z return.


    Hiya CB,

    I just tried your suggestion. No good.
    Ran prog.
    typed Hello, my name is newbyc (then ctrl-z on the same line).
    Then hit enter.
    Prog. terminates with DOS window still open (I added getchar();).
    See code below:

    /* count characters in input; 1st version */

    #include <stdio.h>

    int main(void)
    {
    long nc;

    nc=0;
    while(getchar()!=EOF)
    ++nc;
    printf("%ld\n",nc);
    getchar();
    return 0;
    }
     
    newby2c, Aug 10, 2004
    #19
  20. newby2c

    newby2c Guest

    "Emmanuel Delahaye" <> wrote in message
    news:...
    > I think you must hit <ctrl-z> <enter>


    Hi Emmanuel,

    If you mean to hit <ctrl-z> *and* <enter> at the *same time*, I tried that.
    Unfortunately, that did not work either.
     
    newby2c, Aug 10, 2004
    #20
    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. SL_McManus

    A question about EOF

    SL_McManus, Dec 3, 2003, in forum: Perl
    Replies:
    1
    Views:
    473
    Jim Gibson
    Dec 4, 2003
  2. entropy123

    Newbie Question: EOF in MS Visual Studio 2003?

    entropy123, Jul 2, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,838
    entropy123
    Jul 3, 2003
  3. Kobu
    Replies:
    10
    Views:
    627
    Keith Thompson
    Mar 4, 2005
  4. SpreadTooThin

    ifstream eof not reporting eof?

    SpreadTooThin, Jun 13, 2007, in forum: C++
    Replies:
    10
    Views:
    696
    James Kanze
    Jun 15, 2007
  5. Jan Burse
    Replies:
    67
    Views:
    1,064
    Jan Burse
    Mar 14, 2012
Loading...

Share This Page