3

I am looking for a way to seek to a beginning of stdout. The reason I want to do that is because I am writing a program and i want to constantly display a status(idle, working, done) while writing other output on stdout.

I tried using fseek and lseek but with no success. I also tried using \r and \b in printf when printing but that also didn't work for me.

For example, the output i want to have would be something like this:

[Idle] Waiting for file...

And when the file is received i want it to change the output to:

[Working] Getting contents of the file...

And when that job is complete to change it to:

[Idle] Waiting for file...

[Done] Saved contents from a file...

And when the next file appears change the status to Working and afterwards change it back to Idle and print out a Done message.

Thanks in advance

Community
  • 1
  • 1
b0ne
  • 653
  • 3
  • 10
  • 7
    The contents of stdout are being flushed out so you cannot get back once the data is flushed – Anjaneyulu Nov 29 '16 at 10:31
  • What you could try is store it in a variable. You cannot read a stdout. – Joshua Bakker Nov 29 '16 at 10:36
  • 1
    You mean like store the whole output in a variable (maybe better in file) and then clear the screen and print the whole thing again with the status changed? – b0ne Nov 29 '16 at 10:37
  • 4
    You don't want to seek to beginning of stdout, you want to move the cursor around. For hints, see [How can I erase the current line printed on console in C ?](http://stackoverflow.com/q/1508490/45249) – mouviciel Nov 29 '16 at 10:39
  • Well i would have to erase all lines till beginning so maybe it's better to store the output in a textual file(open the file and change the status with write and lseek) and simply clear the terminal screen and print everything from the file to it. Would something like that be better maybe? – b0ne Nov 29 '16 at 10:43
  • From a UI point of view you want to display both data (file contents) and progress status (idle, working, done). you need a status line somewhere in the terminal. The least intrusive solution would be the xterm window title: `printf("\033]2;%s\007",progress_status)`. – mouviciel Nov 29 '16 at 10:55
  • 1
    You should rethink what you want and whether you really need it. Then remember that stdout is just an output stream, that could be connected to a print terminal (yes, a good old teletype!), and that a rich IHM need more than standard C but graphic or full screen libraries. Everything can be done on any system, but there is nothing like a simple and rich IHM on any system. – Serge Ballesta Nov 29 '16 at 12:51

0 Answers0