Thread: while ((c = getchar()) != EOF), AND cntrl z

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    while ((c = getchar()) != EOF), AND cntrl z

    I want to use getchar() to process a string. Here's a simplified version. I've used this code before to stop entering characters when the user enters the new-line character with no problem:
    Code:
    while ((c = getchar()) != '\n'){    
    	printf("%c", c);
    {
    However, I prefer to read in the newline characters as part of the string. That brings to my question; I don't really understand EOF and "cntrl z". Is entering "cntrl z" in the keyboard suposed to be the same as EOF? The problem is that when the user enters "cntrl z" to simulate (or so i think) EOF, it completely exits the program. Maybe my misunderstanding is simply 'How do i simulate EOF form a windows keyboard?

    This code does not do what I want because the end of the code never gets executed; it just completely exits the program when I press cntrl z. Again, is cntrl z the correct way to simulate EOF on a windows keyboard.
    Code:
    while ((c = getchar()) != EOF){
    	printf("%c", c);
    {
    ...rest of code never gets executed
    Last edited by Roger; 10-21-2009 at 04:21 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's OS dependent. It will either be CTRL Z or CTRL D. Don't listen to people who say it's CTRL C, they're dumb.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    for unix its ctrl+z i think

    and windows ctrl+D, not totally sure

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Obelisk View Post
    for unix its ctrl+z i think

    and windows ctrl+D, not totally sure
    That's backwards. Ctrl-Z on windows, Ctrl-D on UNIX
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    Cntrl D seems to be better but still not working.

    I should have said I'm remotely using a linux system with a Windows PC. Cntrl d seems to be working. Here's the code:
    Code:
    #include <stdio.h>
    
    int main(){
    	char c;
    	while ((c = getchar()) != EOF){
    		printf("%c", c);
    	}
    	printf("test");
    }
    This is exactly what happens:
    1) user enters characters
    2) user presses cntrl-d
    3) prints characters
    4) user presses cntrl-d a second time
    5) prints "test"

    The user should not have to press cntrl-d a second time to execute rest of program. What am I doing wrong?

    Thanks for the help.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    IIRC, both on linux and on windows, Ctrl-<as appropriate> is only EOF if it is the start of the line.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by brewbuck View Post
    That's backwards. Ctrl-Z on windows, Ctrl-D on UNIX
    oh yea, my apologies just checked it out. thnx for the correction

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Incidentally, c should be an int, not a char. Even though the function is called getchar(), it returns an int so that it's able to represent all character values plus EOF. If c is a char, you'll probably see behavior where either a normal character can trigger EOF, or that EOF is never caught.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Location
    USA
    Posts
    63
    Quote Originally Posted by Roger View Post
    I should have said I'm remotely using a linux system with a Windows PC. Cntrl d seems to be working. Here's the code:
    Code:
    #include <stdio.h>
    
    int main(){
    	char c;
    	while ((c = getchar()) != EOF){
    		printf("%c", c);
    	}
    	printf("test");
    }
    This is exactly what happens:
    1) user enters characters
    2) user presses cntrl-d
    3) prints characters
    4) user presses cntrl-d a second time
    5) prints "test"

    The user should not have to press cntrl-d a second time to execute rest of program. What am I doing wrong?

    Thanks for the help.
    as cas said c should be int

    where c contains the ascII value of the variable

    Then if you want to get the char again

    use Putchar(c)

Popular pages Recent additions subscribe to a feed