Thread: scanf function and EOF

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    31

    scanf function and EOF

    Hi
    I write some programm which reading data from stdin until EOF
    Code:
            while (scanf("%s",slowo)!=EOF)
        {
            do something
             }
    and i programm doesn't recognize CTRL+Z i have to press it 3 or 4 times and then while is terminated. Can you tell me please where is mistake and why it is happen?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,811
    Which OS are you on?

    It's typically ctrl-z for DOS/Windows and ctrl-d for Linux/Unix.

    When do you signal EOF?

    hello<ctrl-z>
    is not the same as
    hello
    <ctrl-z>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    31
    I am using windows. The purpose of this programm is to reading data from input until EOF. Input can be number or characters such as +, - and other arithmetic and bitwise operators. That is the reason that i am using scanf. when i am typing for example
    1234/n
    33/n
    43/n
    it works properly but i have to press few times CTRL+Z until programm break the while loop

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,194
    I think on Windows you need to not only enter ctrl-Z as the first "character" on a new line but you also need to press Enter after it.
    If I recall, hex 0x1a (which, being decimal 26, is ctrl-Z) is used as an actual character in a text file that can indicate the end of the file (at least for some programs, such as type). So it seems it's treated as an actual character rather than a signal.
    Last edited by john.c; 1 Week Ago at 01:17 PM.
    We live as it were by chance, and by chance we are governed. - Seneca

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,194
    It would be interesting if someone on Windows could test the following which creates a text file called ctrlz.txt. Once it's created, try "typing" it out in the console with the command type ctrlz.txt to see if it only prints the first two lines.
    Code:
    #include <stdio.h>
    int main() {
        FILE *fout = fopen("ctrlz.txt", "w");
        fprintf(fout, "line one\nline two\n\x1a\nline three\nline four\n");
        fclose(fout);
        return 0;
    }
    We live as it were by chance, and by chance we are governed. - Seneca

  6. #6
    Registered User
    Join Date
    Sep 2020
    Posts
    85
    Yes it prints only the first 2 lines.

  7. #7
    Registered User
    Join Date
    Jun 2019
    Posts
    31
    Nothing work properly even if i will compare slowo[0] to 0x1a.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,194
    Thanks thmm. That's interesting.

    gawiellus, nobody asked you to compare slowo[0] to 0x1a. It is not common for an actual 0x1a to be in the file data. That was just an experiment.

    How about:
    Code:
    char slowo[256];
     
    while (fgets(slowo, sizeof slowo, stdin) != NULL)
    {
        // do something (remembering that slowo retains the newline)
    }
    To end the input enter Ctrl-Z on a separate line, follwed by the Enter key.
    We live as it were by chance, and by chance we are governed. - Seneca

  9. #9
    Registered User
    Join Date
    Jun 2019
    Posts
    31
    I have to use scanf function. It is weird because
    Code:
    while(scanf("%s",slowo)!=EOF)
    works well in linux

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using scanf() function
    By Akbar Mohammed in forum C Programming
    Replies: 2
    Last Post: 10-16-2015, 01:29 PM
  2. while function with scanf function confusion
    By yukapuka in forum C Programming
    Replies: 7
    Last Post: 08-08-2012, 03:49 AM
  3. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  4. tell me the function of scanf();
    By firedream in forum C Programming
    Replies: 4
    Last Post: 11-27-2009, 11:57 AM
  5. need a bit of help with scanf function
    By larkenciel in forum C Programming
    Replies: 1
    Last Post: 09-26-2005, 11:59 AM

Tags for this Thread