seeking in cout text

P

Philip Parker

is there any way to track back x number of characters after its been cout 'd
, and overwrite part of it ?
 
J

John Harrison

Philip Parker said:
is there any way to track back x number of characters after its been cout 'd
, and overwrite part of it ?

No, at least not in standard C++.. You should probably forget about cout and
instead use whatever console functions your platform provides. This is
definitely possible, just not in standard C++.

john
 
J

Jan Engelhardt

is there any way to track back x number of characters after its been cout
'd

No, at least not in standard C++.. You should probably forget about cout and
instead use whatever console functions your platform provides. This is
definitely possible, just not in standard C++.

Maybe using a new class which inherits from cout?

int ::cout(char *s) {
real_cout << s;
return strlen(s);
}

Of course, something like cout << "bla" << "foo" won't work then.





Jan Engelhardt
--
 
J

John Harrison

Jan Engelhardt said:
Maybe using a new class which inherits from cout?

cout is a global variable not a class, so you can't inherit from it.
int ::cout(char *s) {
real_cout << s;
return strlen(s);
}

That code makes no sense to me, but in any case if you don't have the
ability to 'step back' in cout then I can't see that you could somehow get
that ability without using some non-standard C++.

john
 
P

Philip Parker

ah well. was hoping to have some sort of auto-filling progress indicator.
guess i`ll just have to do without , or learn win32 programming heh

thanks
 
J

Jacques Labuschagne

Philip said:
ah well. was hoping to have some sort of auto-filling progress indicator.
guess i`ll just have to do without , or learn win32 programming heh

Well why didn't you say so? You don't need to seek through stdout to do
that, you just need to make it *look* like you're changing the output.
Experiment with the backspace character (\b)... You can do things like

// print [ ] complete
cout << "[ ] complete";
// print [XX ] complete
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bX ] complete";
// print [XXX ] complete
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bX ] complete";
....
// print [XXXXXXXXXX] complete
cout << "\b\b\b\b\b\b\b\b\b\b\bX] complete";


Jacques.
 
P

Philip Parker

ooh, i didnt know about that

thanks :)

Jacques Labuschagne said:
Philip said:
ah well. was hoping to have some sort of auto-filling progress indicator.
guess i`ll just have to do without , or learn win32 programming heh

Well why didn't you say so? You don't need to seek through stdout to do
that, you just need to make it *look* like you're changing the output.
Experiment with the backspace character (\b)... You can do things like

// print [ ] complete
cout << "[ ] complete";
// print [XX ] complete
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bX ] complete";
// print [XXX ] complete
cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bX ] complete";
...
// print [XXXXXXXXXX] complete
cout << "\b\b\b\b\b\b\b\b\b\b\bX] complete";


Jacques.
 
G

Gernot Frisch

Try this on a Win2k machine (don't know if any service pack aleady
fixed it)

int main()
{
for (long i=0; i<256; i++) printf("\b");
return 0;
}

....and get some coffee.
-Gernot
 
A

Andre Heinen

You can also use \r to get back to the left margin and overwrite
everything. Try this:

#include <iostream>
#include <windows.h> // for sleep()
using namespace std;

int main() {
cout << "Hi all!" << flush;
sleep(2);
cout << "\rBye " << flush;
sleep(2);
cout << endl;
}

HTH
 
P

Pete Becker

Andre said:
You can also use \r to get back to the left margin and overwrite
everything.

Just a minor nit: that usually works, but there are output devices that
don't support this, or don't support it in the way you expect. An
ancient example is a mechanical TTY: you can backspace or back up to the
margin, but the text you printed previously is still there, and you end
up double printing.
 
D

David Harmon

On Tue, 25 May 2004 12:07:50 +0100 in comp.lang.c++, "Philip Parker"
ah well. was hoping to have some sort of auto-filling progress indicator.
guess i`ll just have to do without , or learn win32 programming heh

Then why didn't you ask that instead of that stuff about backing up x
characters in cout? Was it supposed to be a secret? You can do a
progress indicator, fine. You would have a hard time backing up
characters in cout.

The usual answer to that is the same in C++ as it is in C, and is
covered in Q. 19.3 of Steve Summit's C FAQ. It is always good to check
the FAQ before posting. You can get the FAQ at:
http://www.eskimo.com/~scs/C-faq/top.html
 
P

PlasmaDragon

Philip Parker said:
is there any way to track back x number of characters after its been cout 'd
, and overwrite part of it ?


Try using the backspace character ('\b'). For example, the following
program gives me the output "cad":

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
cout <<"cat";
cout <<"\b";
cout <<"d";
cout <<endl;

}
 

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

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top