Updating Text on Screen

G

gtpilot

I have a program that is going to return position data several times a
second. Instead of printing this data to the screen on new line every
time, I would like to simply have the program print "Position: XX" on
one line, adn then update the XX every time there is new data.

I am working with just a simple console app in Linux.

Someone had mention fstream and seekp but I was wondering if anyone had
a bit more guidance or somewhere to look, perhaps a good primer on
using fstream with the screen buffer (almost everything about fstream
that I found relates to using it with a file).

Thanks
 
V

Victor Bazarov

I have a program that is going to return position data several times a
second. Instead of printing this data to the screen on new line every
time, I would like to simply have the program print "Position: XX" on
one line, adn then update the XX every time there is new data.

Outputting

std::cout << "\rPosition: " << yourpositionvariable;

inside your loop will probably do what you want. Don't forget to finish
the overall output with a single newline.
I am working with just a simple console app in Linux.

Then you might want to post to 'comp.os.linux.development.apps'.
Someone had mention fstream and seekp but I was wondering if anyone
had a bit more guidance or somewhere to look, perhaps a good primer on
using fstream with the screen buffer (almost everything about fstream
that I found relates to using it with a file).

There are several books on standard library available, all decent. One
deals especially with streams, the authors are Kuehl and Langer. I'm not
sure whether your implementation of standard output linked to the screen
(console) is capable of positioning (and what effect it's going to have),
you would need to ask in the newsgroup dedicated to your implementation
or library (or OS).

V
 
P

phlip

gtpilot said:
I have a program that is going to return position data several times a
second.

Google 'ncurses'.

Then Google for the best forum for these questions. This newsgroup is only
qualified to discuss raw C++, which has no text-positioning facilities.
 
R

Rolf Magnus

Victor said:
Outputting

std::cout << "\rPosition: " << yourpositionvariable;

inside your loop will probably do what you want.

The stream should be flushed at the end. Otherwise, the update is probably
done too late:

std::cout << "\rPosition: " << yourpositionvariable << std::flush;
 
V

Victor Bazarov

Rolf said:
The stream should be flushed at the end. Otherwise, the update is
probably done too late:

std::cout << "\rPosition: " << yourpositionvariable << std::flush;

There is no guarantee that \r does what the OP needs, but if it does,
it most likely will do it without flush (IME, anyway).

V
 
R

Rolf Magnus

Victor said:
There is no guarantee that \r does what the OP needs, but if it does,
it most likely will do it without flush (IME, anyway).

I have no idea if \r usually flushes the stream, but if it does, it will do
it before the line gets written, not afterwards. This might not be very
relevant if you print the line "several times a second", but I would add
the flush anyway.
 
G

gtpilot

Won't all of these just print out

Your Position: X1
Your Position: X2
Your Position: X3
Your Position: X4
..
..
..

etc all the way to Xreally big number?

I just want the program to print 1 line to the console during the
entire program, and then go back and basically place the cursor where
the numbers start and write the current value.

Someone suggested using \b to backspace, but that ends up adding some
really wierd cursus images and flickering.
 
V

Victor Bazarov

Won't all of these just print out

Your Position: X1
Your Position: X2
Your Position: X3
Your Position: X4
.
.
.

etc all the way to Xreally big number?

(a) Don't top-post.
(b) Why don't you simply try and see instead of asking?
I just want the program to print 1 line to the console during the
entire program, and then go back and basically place the cursor where
the numbers start and write the current value.

You don't have to repeat yourself. We get it. You need the program to
print everything on one line. Go try outputting \r and see what happens.
Someone suggested using \b to backspace, but that ends up adding some
really wierd cursus images and flickering.

Yes, and it's possible that \r will not do what you want. There is no
sure way in *Standard* C++ to do what you want. That's why you were
given the advice to look into 'ncurses' library.

Meanwhile try this:

#include <iostream>

int main() {
for (int i = 0; i < 1000000; ++i)
std::cout << "\rValue = " << i;
std::cout << std::endl;
}

What do you see?

V
 
G

gtpilot

(a) Don't top-post.

My mistake, didn't know it made any difference
(b) Why don't you simply try and see instead of asking?

Again my mistake, I must have misunderstood something along the line.
Yes that \r works for my purposes, and I may uses ncurses i in the long
run.

Thanks for your help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top