Eof

This is a discussion on Eof within the C Programming forums, part of the General Programming Boards category; Hi, I'v got a problem with runnig of this program. It doesn't work. My input comes from keyboard and I ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Eof

    Hi,
    I'v got a problem with runnig of this program. It doesn't work. My input comes from keyboard and I can only input characters and the characters never get counted.
    I think it relates with EOF.
    Could you help me please?

    Code:
    /*char_count.c. A program to count characters of input.*/
    #include <stdio.h>
    main()
    {
    int c ;
    int count ;
    count=0;
    while ( ( c = getchar() ) != EOF )
    count ++count ;
    printf( "%d characters\n" , count ) ;
    }

  2. #2
    Registered User
    Join Date
    Feb 2005
    Posts
    38
    Okay, you type the EOF character on your keyboard, then come back and talk to us.

    First off, main should ALWAYS return an int even if your compiler let's you get away with not returning it.

    Second, I think this is what you're looking for:

    Code:
    #include <stdio.h>
    int main(void)
    {
      int c ;
      int count ;
      count=0;
      while ( ( c = getchar() ) != '\n' )
          ++count ;
      printf( "%d characters\n" , count );
      return 0;
    }
    Yay!

    Code:
    nouse@NeverlandRanch:~$ gcc count.c -o count      
    nouse@NeverlandRanch:~$ ./count           
    abcd
    4 characters
    nouse@NeverlandRanch:~$
    I like to play pocket pool.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    Hi,
    Thanks for the information. But I aks me how it is if a text has several rules (you've used "Enter" to make a text)?
    For example:
    "This is a test.
    I want to control the above program."

    With your program you can only count the first rule, can't you?
    bye

  4. #4
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    In most cases you won't have multiline input from the command line, but from a file, and there you don't hit enter to input, obviously, and can read till you find EOF.

    As for allowing the user to type say two lines and count the characters in, as in your example... hm, wasn't that easy. I can't find any clever solution off the bat - all functions for input seem to start reading as soon as you hit enter.

    You could of course read one line, then just let the user keep typing and read the other line, but then you don't know when there is nothing more to read.

    -edit: I edited because my tests that I used to determine what I wrote here were faulty, and thus my writings were as well.
    Last edited by Silfer; 11-27-2005 at 04:07 PM.
    -S

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, 08: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, 03: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

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