check input from standard input stream?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lini
    New Member
    • Mar 2007
    • 12

    check input from standard input stream?

    Hello,
    I am writing some code in the scenario which can be described as follow:
    + program A which writes to standard output (e.g. cout >> whatever).
    + program B which has GUI and also listens to the standart input regularly (using a timer) if there is no input, it returns to the GUI and does something else.

    What I expect is B should work without problem in both cases:
    (1) user@host $ A | B
    (2) user@host $ B

    In B's code, I tried using: "cin >> var" or "scanf("%d" , &var)" to check if there is something from input. if nothing, go back and response to user interactions on the GUI.

    If I use cin >> var or scanf("%d", &var) to try reading from standart input as the following:

    if (cin >> var) {
    // do smt here
    }

    or

    if (scanf("%d", &var)) {
    // do smt here
    }

    even in case (1), nothing is read. Did I listen to the wrong stream? And it seems to me that cin and scanf wait for something from standard input and if there is nothing, they keep waiting and the program just hangs. It does not make sense because I think that if there is nothing in the input stream, the read will fail and the program moves on. Is that right?

    Is there some way in C++ that we can check the content of the (input) stream before really extract values? are cin.eof() are cin.rdstate() useful in this case?

    Thank you very much and looking forward to hearing from you.

    lini
Working...