I opened two terminals (/dev/pts/1 and /dev/pts/2) and started my application from /dev/pts/1. I want to read in real time from /dev/pts/2 but my code doesn't work: actually some of the symbols are shown on the /dev/pts/1 and some of them are shown on the /dev/pts/2

FILE *f = fopen("dev/pts/2", "r");
while(1) {
    char current = fgetc(f);
    printf("%c", current);

You have two processes reading from /dev/pts/2. One is the shell (or some application) running there, the other is your application on pts/1. It's random which one is faster reading the available bytes.

  • So I can do nothing? – WildWind03 May 7 '16 at 12:45
  • Make sure noone else is reading from there. What's your ultimate goal? – egmont May 7 '16 at 12:48
  • If you want to have your app's input and output at two different terminals, it's easier to run the app on the terminal where you're about to type the input, and redirect the output to the other one. – egmont May 7 '16 at 12:53
  • I'm writing an application which reads lines from some files (in such order: first line from first file, first line from second file, ..., second line from first file etc.). Some files can be terminals so I have to read lines from them. – WildWind03 May 7 '16 at 12:54
  • If you don't want the shell and your program competing for input, type sleep 99999 to the shell. – Mark Plotnick May 7 '16 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.