Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending data to stdin of another process through linux terminal

Tags:

c++

linux

bash

pipe

I've been trying to send data to stdin of a running process. Here is what I do:

  1. In a terminal I've started a c++ program that simply reads a string and prints it. Code excerpt:

    while (true) {
        cin >> s;
        cout << "I've just read " << s << endl;
    }
    
  2. I get the PID of the running program

  3. I go to /proc/PID/fd/
  4. I execute echo text > 0

Result: text appears in the terminal where the program is run. Note, not I've just read text, but simply text. What am I doing wrong and what should I do to get this thing to print 'I've just read text'?

like image 807
Vlad Sobol Avatar asked Oct 16 '25 16:10

Vlad Sobol


1 Answers

When you're starting your C++ program you need to make sure its input comes from a pipe but not from a terminal. You may use cat | myapp to do that. Once it's running you may use PID of your application for echo text > /proc/PID/fd/0

like image 52
Mikhail Volskiy Avatar answered Oct 18 '25 07:10

Mikhail Volskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!