Thread: How to use EOF??

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    How to use EOF??

    I am really a novice in C programming, this is a program from KnR2
    It counts the number of characters in the input

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

    and here's the output-

    RedDragon:~ test$ gcc chrcnt.c
    RedDragon:~ test$ ./a.out
    igsafkg
    isgfg

    so, as you can see I cannot get the print as the EOF is not getting encountered by the flow, how do I make this program work?? I use Xcode for compiling and terminal on apple macintosh for doing my work.

    -RED

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try hitting CTRL-D when you have finished typing the text. That is "end of text", which is used at least in Unix to signify end of file on devices that do not have a physical length stored (console is one of those).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    9

    Thumbs up

    well, that seems to work, thank you..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM