EOF not working?

This is a discussion on EOF not working? within the C Programming forums, part of the General Programming Boards category; Hey, I'm reading "The C Programming Language" and making sure I experiment with every program example there is in it. ...

  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
    2,577
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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
    2,577
    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.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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, 11:31 AM
  4. When working on a pc....
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-18-2003, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21