Thread: EOF not working?

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    Question EOF not working?

    Hey,

    I'm reading "The C Programming Language" and making sure I experiment with every program example there is in it.
    In the beggining I've come across one that's supposed to count the characters in the input.
    When I try to write and run it myself, it doesn't print what it is supposed to and apparently it is looping on the 'while' part and I suspect it is because of the EOF test.

    Here's the code, please, someone help me!

    PS.: I'm using NetBeans IDE/Cygwin

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* Counts characters in input 
     */
    
    int main(int argc, char** argv) {
        
        long nc;
        
        nc = 0;
        while(getchar() != EOF){
            nc++;
        }
        printf("%d\n", nc);
    
    
        return (EXIT_SUCCESS);
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you try entering EOF?
    If I recall correctly, Control-D or Control-Z is normally EOF.

    Note: Sometimes it requires the ENTER key after the EOF is entered.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    You mean entering EOF after the text it's supposed to count the characters of?

    I just tried typing in the text, then CTRL-D or CTRL-Z, then hit Enter but nothing happened. :/

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    71
    will do stuff until reach the end, try this
    Code:
    while (!eof()){
    stuff
    }
    you also forgot the scopes, eof() is function

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by CoffeCat View Post
    will do stuff until reach the end, try this
    Code:
    while (!eof()){
    stuff
    }
    you also forgot the scopes, eof() is function
    @CoffeCat Please read; or give a better example because your example like like what NOT to do.
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working my way up
    By GReaper in forum Projects and Job Recruitment
    Replies: 7
    Last Post: 04-09-2010, 02:20 AM
  2. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  3. I have tried a lot...but this is not working.
    By nomi in forum C Programming
    Replies: 19
    Last Post: 01-13-2004, 12:31 PM
  4. When working on a pc....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-18-2003, 08:31 AM
  5. Why is this not working??? PLEASE HELP ANYONE!!!!
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 06-26-2002, 01:06 PM