Connecting Tech Pros Worldwide Forums | Help | Site Map

Updating Text on Screen

gtpilot@gmail.com
Guest
 
Posts: n/a
#1: Jun 13 '06
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


Victor Bazarov
Guest
 
Posts: n/a
#2: Jun 13 '06

re: Updating Text on Screen


gtpilot@gmail.com wrote:[color=blue]
> 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.[/color]

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.
[color=blue]
> I am working with just a simple console app in Linux.[/color]

Then you might want to post to 'comp.os.linux.development.apps'.
[color=blue]
> 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).[/color]

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


phlip
Guest
 
Posts: n/a
#3: Jun 13 '06

re: Updating Text on Screen


gtpilot wrote:
[color=blue]
> I have a program that is going to return position data several times a
> second.[/color]

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
Rolf Magnus
Guest
 
Posts: n/a
#4: Jun 13 '06

re: Updating Text on Screen


Victor Bazarov wrote:
[color=blue]
> gtpilot@gmail.com wrote:[color=green]
>> 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.[/color]
>
> Outputting
>
> std::cout << "\rPosition: " << yourpositionvariable;
>
> inside your loop will probably do what you want.[/color]

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

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

Victor Bazarov
Guest
 
Posts: n/a
#5: Jun 13 '06

re: Updating Text on Screen


Rolf Magnus wrote:[color=blue]
> Victor Bazarov wrote:
>[color=green]
>> gtpilot@gmail.com wrote:[color=darkred]
>>> 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.[/color]
>>
>> Outputting
>>
>> std::cout << "\rPosition: " << yourpositionvariable;
>>
>> inside your loop will probably do what you want.[/color]
>
> The stream should be flushed at the end. Otherwise, the update is
> probably done too late:
>
> std::cout << "\rPosition: " << yourpositionvariable << std::flush;[/color]

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


Rolf Magnus
Guest
 
Posts: n/a
#6: Jun 13 '06

re: Updating Text on Screen


Victor Bazarov wrote:
[color=blue]
> Rolf Magnus wrote:[color=green]
>> Victor Bazarov wrote:
>>[color=darkred]
>>> gtpilot@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.[/color]
>>
>> The stream should be flushed at the end. Otherwise, the update is
>> probably done too late:
>>
>> std::cout << "\rPosition: " << yourpositionvariable << std::flush;[/color]
>
> 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).[/color]

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.

gtpilot@gmail.com
Guest
 
Posts: n/a
#7: Jun 13 '06

re: Updating Text on Screen


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:[color=blue]
> Victor Bazarov wrote:
>[color=green]
> > Rolf Magnus wrote:[color=darkred]
> >> Victor Bazarov wrote:
> >>
> >>> gtpilot@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;[/color]
> >
> > 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).[/color]
>
> 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.[/color]

Victor Bazarov
Guest
 
Posts: n/a
#8: Jun 14 '06

re: Updating Text on Screen


gtpilot@gmail.com wrote:[color=blue]
> 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?[/color]

(a) Don't top-post.
(b) Why don't you simply try and see instead of asking?
[color=blue]
> 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.[/color]

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.
[color=blue]
> Someone suggested using \b to backspace, but that ends up adding some
> really wierd cursus images and flickering.[/color]

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?
[color=blue]
> [..][/color]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


gtpilot@gmail.com
Guest
 
Posts: n/a
#9: Jun 14 '06

re: Updating Text on Screen


> (a) Don't top-post.

My mistake, didn't know it made any difference
[color=blue]
> (b) Why don't you simply try and see instead of asking?[/color]

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

Closed Thread