Results 1 to 7 of 7

Thread: stdin reading

  1. #1
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    3

    Default stdin reading

    hi,

    i'd like to read stdin like this:

    Qt Code:
    1. QFile in;
    2. in.open(stdin, QIODevice::ReadOnly);
    3. QString line = in.readLine();
    To copy to clipboard, switch view to plain text mode 

    But if no data is written to stdin (by another process) then it hangs...
    How can i determine if there is anything to read or not?
    in.atEnd() does not work, in.bytesAvailable() does not work... they always return 0, even if the other process has written data to stdin.
    So before calling in.readLine() i would like to know if there is data available, because i want QString line to be empty, if stdin is empty.

    Thanks!
    Oh and it is win32, i've read QSocketNotifier can be used in linux to notify if data has been written to stdin...

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: stdin reading

    why not use cin and `operator >> ` ?

    for the socket trick you have to make sure the buffer is not used. I didn't know that trick was linux only.

    you can try this,
    Qt Code:
    1. QTextStream qtin(stdin);
    2. QString line = qtin.readLine(); // This is how you read the entire line
    3.  
    4. QString word;
    5. qtin >> word; // This is how you read a word (separated by space) at a time.
    To copy to clipboard, switch view to plain text mode 
    as suggested here http://stackoverflow.com/questions/2...use-cin-and-qt
    Last edited by amleto; 26th July 2012 at 23:54.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. The following user says thank you to amleto for this useful post:

    gmc (26th July 2012)

  4. #3
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    3

    Default Re: stdin reading

    Thanks.
    i've already tried that, still waits until data is written to stdin

  5. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: stdin reading

    in that case get the rdbuf from cin.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  6. The following user says thank you to amleto for this useful post:

    gmc (27th July 2012)

  7. #5
    Join Date
    Jul 2012
    Posts
    4
    Thanks
    3

    Default Re: stdin reading

    still waits... could you write some more details how to do that?

    i've tried this too:

    Qt Code:
    1. std::string line;
    2. std::getline( std::cin, line );
    To copy to clipboard, switch view to plain text mode 

    But still hangs and waits for data... it's so annoying. Why can't just read an empty line if nothing is there? or at least .bytesAvailable could be non-zero when i write to the stdin so i could check before reading..

  8. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: stdin reading

    I said try and use rdbuf. I don't see you use rdbuf anywhere. It is a member of cin

    http://www.cplusplus.com/reference/iostream/ios/rdbuf/


    I have just tried it, and it does not block.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. The following user says thank you to amleto for this useful post:

    gmc (31st July 2012)

  10. #7
    Join Date
    May 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: stdin reading

    to
    amleto

    Could you show an example how you used rdbuf to read from stdin and not block main event loop, please? Thanks in advance

Similar Threads

  1. Write to QProcess from stdin
    By rajji_saini in forum Newbie
    Replies: 2
    Last Post: 24th October 2011, 18:45
  2. QProcess and stdin
    By ithinkso in forum Newbie
    Replies: 7
    Last Post: 23rd May 2011, 06:21
  3. Replies: 0
    Last Post: 2nd August 2010, 09:17
  4. Looking to NOT echo input when writing to stdin
    By ultim8 in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2009, 21:57
  5. IPC / reading stdin within non-GUI thread
    By mule in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2007, 22:11

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.