Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Console Application: unexpected QTextStream(stdin) behavior
Qt 6.11 is out! See what's new in the release blog

Console Application: unexpected QTextStream(stdin) behavior

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ScottCUSA
    wrote on last edited by
    #1

    @
    QTextStream qtout(stdout);
    QTextStream qterr(stderr);
    QTextStream qtin(stdin);

    QString getFileName() {

    QString fileName = "";
    do {
        qtout << "Enter a filename: " << flush;
        fileName = qtin.readLine();
        qtout << "You entered: \"" << fileName << "\"" << endl << endl;
    } while (fileName.isEmpty()); // the user must enter a filename
    
    //check to see if the file exists
    QFile data(fileName);
    if(data.exists()) { //this the file exists
        qtout << "A file with that name already exists. " << endl
              << "If you continue it will be over-written." << endl
              << "Do you wish to continue? " << endl
              << "1) yes \n2) no \n3) to change filename" << endl
              << endl;
    
        int choice = 0;
        do {
            qtout << "Enter 1, 2 or 3: " << flush;
            qtin >> choice;
            qtout << endl << flush;
            if(qtin.status() != QTextStream::Ok) {
                qtin.reset();
                qtin.resetStatus();
                QString garbage = "";
                garbage = qtin.readLine();
                qtout << "You must enter a number!" << endl;
            }
        } while (!(choice == 1 or choice == 2 or choice == 3));
    
        if (choice == 1) {
            return fileName;  //overwrite the file
        } else if (choice == 2) {
            return QString(); //user does not wish to continue
        } else {
            return getFileName(); // recursively attempt to get a good filename
        }
    }
    return fileName;  //file did not exist (hopefully)
    

    }@

    Output:
    @Enter a filename: vitr
    You entered: "vitr"

    A file with that name already exists.
    If you continue it will be over-written.
    Do you wish to continue?

    1. yes
    2. no
    3. to change filename

    Enter 1, 2 or 3: 3

    Enter a filename: You entered: ""

    Enter a filename: vitry
    You entered: "vitry"

    A file with that name already exists.
    If you continue it will be over-written.
    Do you wish to continue?

    1. yes
    2. no
    3. to change filename

    Enter 1, 2 or 3: 3

    Enter a filename: You entered: ""

    Enter a filename: vitryu
    You entered: "vitryu"

    Read data from the file - watch for errors.@

    Notice how on line 13 and on line 27 of the output the program doesn't wait for input. Yet on the initial call of the function it waits properly. I don't understand why it doesn't stop and wait for input on line 6 when the function is called recursively. Can anyone help me understand why it's doing this or help me fix it?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, when you press 1, 2 or 3, what you have pressed is

      @
      1\n, 2\n or 3\n
      @

      And you forget to discard the \n before you call readLine()

      1 Reply Last reply
      0
      • S Offline
        S Offline
        ScottCUSA
        wrote on last edited by
        #3

        Thank you!

        That makes a lot of sense. It just seems like that trailing '\n' should be discarded from the stream. How would you suggest I discard the '\n'?

        readLine()?

        This works, I was just wondering what you would suggest?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved