By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,407 Members | 920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,407 IT Pros & Developers. It's quick & easy.

Updating Text on Screen

P: n/a
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

Jun 13 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
gt*****@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #2

P: n/a
gtpilot wrote:
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.

--
Phlip
Jun 13 '06 #3

P: n/a
Victor Bazarov wrote:
gt*****@gmail.com wrote:
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.


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

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

Jun 13 '06 #4

P: n/a
Rolf Magnus wrote:
Victor Bazarov wrote:
gt*****@gmail.com wrote:
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.


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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #5

P: n/a
Victor Bazarov wrote:
Rolf Magnus wrote:
Victor Bazarov wrote:
gt*****@gmail.com wrote:
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.


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).


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.

Jun 13 '06 #6

P: n/a
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.

Rolf Magnus wrote:
Victor Bazarov wrote:
Rolf Magnus wrote:
Victor Bazarov wrote:

gt*****@gmail.com wrote:
> 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.

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).


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.


Jun 13 '06 #7

P: n/a
gt*****@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 13 '06 #8

P: n/a
> (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

Jun 14 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.