std out question

A

Alf P. Steinbach

* glen_stark said:
maybe this is more an iostream question.

I would like to output a value to a console in the following manner:

value: xx

Where xx gets updated dynamically. Is it possible using std C++ to back
up to a certain position for output?

C++ provides no such facility. That would be OK, except C++ also actively
sabotages the facility that in practice is universally available, namely
the ASCII carriage return character (ASCII character 13, which is denoted by
'\r' in C++ (but note that '\r' is not necessarily denoted by ASCII 13)).
The sabotage takes the form of possible translation of the carriage return
character, and that this possible translation can not be turned off for
the standard named iostreams, although it can be turned off for other streams.

As a consequence of that sabotage you cannot, in standard C++, write a program
that copies its standard input exactly to its standard output, like 'cat'.

I think that's horrible, but whenever it has been mentioned a very large flock
of C++ zealots have descended on the thread in question, so it's a bit touchy.

If your output is to a typical (?) text terminal, however, you can probably
use an escape sequence. Such sequences vary from terminal to terminal, but
there is an ANSI standard. If I recall correctly you'd output ASCII 27, the
escape character, followed by '[', followed by e.g. 'H' to move the text
cursor to the upper left corner of the screen -- and other sequences to
move it elsewhere, set colors, etc.

Possibly, if your output is to an emulation of a terminal in a GUI window, it
might be possible to use an ANSI driver in between your program output and the
actual presention -- e.g. there was such a driver, called ansi.sys, in
16-bit Windows, but as far as I know there is no such 32-bit Windows driver.
 
D

David Harmon

On Mon, 12 Apr 2004 12:54:17 GMT in comp.lang.c++, (e-mail address removed) (Alf
P. Steinbach) wrote,
C++ provides no such facility. That would be OK, except C++ also actively
sabotages the facility that in practice is universally available, namely
the ASCII carriage return character (ASCII character 13, which is denoted by
'\r' in C++ (but note that '\r' is not necessarily denoted by ASCII 13)).
The sabotage takes the form of possible translation of the carriage return
character, and that this possible translation can not be turned off for
the standard named iostreams, although it can be turned off for other streams.

It is certainly true that C++ allows an implementation to stomp all over
'\r' or for that matter any other character that you might send to a
text output stream (translate from ASCII to EBCDIC, or whatever.)
But, in practice, '\r' seems to be usually left alone and function as
you might hope. '\n' is of course another matter entirely, and '\r' on
input is also questionable. So, I think using '\r' is a better bet than
looking for terminal-defined escape sequences and hoping that the output
stream won't sabotage them. At least I would try it first.
 
O

osmium

David said:
On Mon, 12 Apr 2004 12:54:17 GMT in comp.lang.c++, (e-mail address removed) (Alf
P. Steinbach) wrote, streams.

It is certainly true that C++ allows an implementation to stomp all over
'\r' or for that matter any other character that you might send to a
text output stream (translate from ASCII to EBCDIC, or whatever.)
But, in practice, '\r' seems to be usually left alone and function as
you might hope. '\n' is of course another matter entirely, and '\r' on
input is also questionable. So, I think using '\r' is a better bet than
looking for terminal-defined escape sequences and hoping that the output
stream won't sabotage them. At least I would try it first.

Does the word "sabotaged" mean anything at all to you? You would go through
a hell of a lot of platforms before you found one where CR worked as
intended by the designers of ASCII. My guess is that you would never find
one unless it was in a museum someplace. The OP wants the gotoxy() function
which is probably in conio or ncurses. He should post to a platform
specific newsgroup for further information.
 
D

David Harmon

On Mon, 12 Apr 2004 11:55:39 -0700 in comp.lang.c++, "osmium"
Does the word "sabotaged" mean anything at all to you? You would go through
a hell of a lot of platforms before you found one where CR worked as
intended by the designers of ASCII.

What do you think was intended by the designers of ASCII? What were
their names? Bitch.
My guess is that you would never find
one unless it was in a museum someplace.

I compiled the following program with MS Visual C++ version 6.0.
Of course many would say it belongs in a museum someplace, but in fact
many people still use it.

#include <iostream>
int main()
{
for(int i=0; i<500; i++)
{
std::cout << "\rValue: " << i;
}
}

When I run it, I see "Value: " followed by a number in one place on my
screen that is counting up rapidly to 499. This is exactly what the OP
asked for. Owners of non-museum computers may see only the final
result.

I compiled and ran the same program with the free Digital Mars compiler
http://www.digitalmars.com with exactly the same results.
The OP wants the gotoxy() function
which is probably in conio or ncurses. He should post to a platform
specific newsgroup for further information.

He got the information he asked for. If he wants something beyond that,
a platform specific newsgroup is a good idea.
 
U

Unforgiven

David Harmon said:
On Mon, 12 Apr 2004 11:55:39 -0700 in comp.lang.c++, "osmium"


What do you think was intended by the designers of ASCII? What were
their names? Bitch.


I compiled the following program with MS Visual C++ version 6.0.
Of course many would say it belongs in a museum someplace, but in fact
many people still use it.

#include <iostream>
int main()
{
for(int i=0; i<500; i++)
{
std::cout << "\rValue: " << i;
}
}

When I run it, I see "Value: " followed by a number in one place on my
screen that is counting up rapidly to 499. This is exactly what the OP
asked for. Owners of non-museum computers may see only the final
result.

Although I had to add a Sleep(50); (from <windows.h>) to actually see
anything on this P4 2667MHz, the same result is achieved with Visual C++
..Net 2003.
 
G

glen_stark

maybe this is more an iostream question.

I would like to output a value to a console in the following manner:

value: xx

Where xx gets updated dynamically. Is it possible using std C++ to back
up to a certain position for output? (Is it clear what I'm asking
for?). Something to do with an output stream and backup up the output
stream?

Any advice would be appreciated.

Glen
 
M

Mike Wahler

glen_stark said:
Sort of. I don't program exclusively on one platform, and I very often
program applications which should run on multiple platforms. In fact
I'm woefully ignorant of non portable libraries, and hope to be able to
remain so. So I am looking for (and asked for) a way to get some
functionality under standard C++ that allows me to keep a running update
of some value (one value at a time is sufficient), so that I don't have
pages and pages of difficult to parse (human eye parse) output.

I often find I would like to be able to do this, so my desire for a
standard compliant method to do this transcends my current project,
which is in any case meant to be compiled with various different
compilers to test for optimization issues. Further, it will have to run
on both windows and linux machines, and in an ideal world, I'd like the
results to be the same if they are running it from a dos shell, cygwin
shell, or linux shell. So I don't think posting to a platform specific
newsgroup would be helpful.

Really, just the ability to work with a single line of output at a time,
being able to back up to a certain point and update the value, instead
of a new line of output every time, would be sufficient for my needs.
The results I'm looking for are like the output for the par2 parity
checker.

The \r thing sounds like a good idea, and I'll try it.

As others have pointed out, there is not standard portable
way to do what you're asking. As for writing 'easy-to-read'
code, factor out this kind of platform-specific stuff into
separate modules, and choose a common name for all such
functions (link to the appropriate one for each target
platform), then your 'main' code will be 'cleaner'.

-Mike
 
G

glen_stark

The OP wants the gotoxy() function
which is probably in conio or ncurses. He should post to a platform
specific newsgroup for further information.

Sort of. I don't program exclusively on one platform, and I very often
program applications which should run on multiple platforms. In fact
I'm woefully ignorant of non portable libraries, and hope to be able to
remain so. So I am looking for (and asked for) a way to get some
functionality under standard C++ that allows me to keep a running update
of some value (one value at a time is sufficient), so that I don't have
pages and pages of difficult to parse (human eye parse) output.

I often find I would like to be able to do this, so my desire for a
standard compliant method to do this transcends my current project,
which is in any case meant to be compiled with various different
compilers to test for optimization issues. Further, it will have to run
on both windows and linux machines, and in an ideal world, I'd like the
results to be the same if they are running it from a dos shell, cygwin
shell, or linux shell. So I don't think posting to a platform specific
newsgroup would be helpful.

Really, just the ability to work with a single line of output at a time,
being able to back up to a certain point and update the value, instead
of a new line of output every time, would be sufficient for my needs.
The results I'm looking for are like the output for the par2 parity
checker.

The \r thing sounds like a good idea, and I'll try it.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top