Thread: Terminating console input in C and C++

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    12

    Terminating console input in C and C++

    Hi, I was having this exercise where I had to write some input/output functions in C++ but call C functions to do the job and throw an exceptions if errors occurred.

    So now I have to write to file using fprintf and I suppose I'll use scanf to get input from console

    When I was reading file I used this simple code (don't know if it's the best way to go)

    Code:
    FILE* fp = open_file(filename, "r");
    
    for (char ch = 0; fscanf(fp, "%c", &ch) != EOF;)
        printf("%c", ch);
    I think than when I'm getting some random text from console I also have to work with chars coz I don't know if next thing I'm getting is char or float or anything else from the input

    When I have to do something like this in C++ I would use something like this


    Code:
    for(std::string line; std::getline(std::cin, line);){
        if(line == "__STOP__") break;   // woud use some keyword to terminate input
    
        //do something with line for example write line to some file
    }
    Is there some smart way to do the above example without using some terminating string?

    How can I read some long random texts from console using scanf("%c", &ch)?

    Is there a way to trigger EOF while using code something like this
    Code:
    while(scanf("%c", &ch) != EOF){
    //do something with char
    }
    Is there a way to trigger std::cin.eof() if I'm using std::getline(std::cin, line) or even std::cin >> ch //char ch;

    Any help appreciated.

    EDIT : Why is my code looking so ugly except the middle one? Same tags around all of them.
    Last edited by etrusks; 01-05-2016 at 06:42 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes. Redirect input from a file, or trigger it with CTRL + D or CTRL + Z.

    By the way, it is usually better to compare the return value of scanf with the number of expected assignments rather than with EOF.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    Cool tnx you very much!

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    Uhh Im having again 1 quick question about this. So here is the code that ask for console input and write it to file

    Code:
    void write_to_file(const std::string& filename) {
    
    
        FILE* fp = open_file(filename, "w"); // throws exception in case cant open
    
    
        for (char ch = 0; scanf("%c", &ch) == 1;) {
            fprintf(fp, "%c", ch);
        }
    
    
        close_file(fp);                        // throws exception in case cant close
    }
    Im using ctrl + z and it is kind of working but if I write to console something like "this is my text" than press ctrl + z and than press enter, program still waits for me to write something in console. Than if i press ctrl + z again and press enter again only than I get out of this for loop.

    If my code wrong that I cant exit when I'm using ctrl + z for the first time?

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How are you entering the data? If you're not pressing return after entering each character you will need to enter the end of file character twice. The end of file character must be the first thing entered on a line to trigger the failure of the stream.

    By the way your comments are incorrect. Exceptions are a C++ feature, and even C++ doesn't throw an exception if the file doesn't open properly.

    Jim

  6. #6
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    I am entering whole line and only in the end I'm pressing ctrl + z and it displays something like "^Z" in console. Only than I'm pressing enter and its not working the 1st time

    About exceptions - it was my exercise to create C++ functions that throw exceptions in case C functions cant open the files or do something else wrong.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I am entering whole line and only in the end I'm pressing ctrl + z and it displays something like "^Z" in console. Only than I'm pressing enter and its not working the 1st time
    That's normal. The end of file character must be the first character entered to trigger EOF.

    About exceptions - it was my exercise to create C++ functions that throw exceptions in case C functions cant open the files or do something else wrong.
    Then why are you playing with scanf() in that loop. Just get a single string and then print that string to the file.

    I also don't understand why you want to "throw exceptions in case C functions cant open the files or do something else wrong.", just don't use these C functions, stick with C++ streams.


    Edit: Also posted here. I'm done.
    Jim
    Last edited by jimblumberg; 01-05-2016 at 09:01 AM.

  8. #8
    Registered User
    Join Date
    Jan 2016
    Posts
    12
    Ok tnx a lot for info. I was supposed to use C functions in my exercise that's why I'm using fprintf() and scanf()

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating input upon encountering a blank line
    By thames in forum C++ Programming
    Replies: 7
    Last Post: 12-21-2012, 12:00 PM
  2. Terminating input on EOF
    By vyn in forum C Programming
    Replies: 7
    Last Post: 03-12-2009, 02:33 AM
  3. terminating input
    By Chaplin27 in forum C++ Programming
    Replies: 1
    Last Post: 10-07-2004, 05:32 PM
  4. terminating input by ctrl+z
    By ammar in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2002, 06:03 AM
  5. Replies: 2
    Last Post: 03-12-2002, 02:32 PM

Tags for this Thread