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

seeking in cout text

P: n/a
is there any way to track back x number of characters after its been cout 'd
, and overwrite part of it ?
Jul 22 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a

"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message
news:c8**********@news7.svr.pol.co.uk...
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
Jul 22 '05 #2

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


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

Jul 22 '05 #3

P: n/a

"Jan Engelhardt" <je*****@linux01.gwdg.de> wrote in message
news:Pi*******************************@yvahk01.tjq t.qr...
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 andinstead 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?


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
Jul 22 '05 #4

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

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...

"Jan Engelhardt" <je*****@linux01.gwdg.de> wrote in message
news:Pi*******************************@yvahk01.tjq t.qr...
> 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
andinstead 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?


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

Jul 22 '05 #5

P: n/a
Philip Parker wrote:
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.
Jul 22 '05 #6

P: n/a
ooh, i didnt know about that

thanks :)

"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:kZ******************@news02.tsnz.net...
Philip Parker wrote:
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.

Jul 22 '05 #7

P: n/a
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
Jul 22 '05 #8

P: n/a
>"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:kZ******************@news02.tsnz.net...
Experiment with the backspace character (\b)...


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

--
Andre Heinen
My address is "a dot heinen at europeanlink dot com"
Jul 22 '05 #9

P: n/a
Andre Heinen wrote:
"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:kZ******************@news02.tsnz.net...
Experiment with the backspace character (\b)...


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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #10

P: n/a
On Tue, 25 May 2004 12:07:50 +0100 in comp.lang.c++, "Philip Parker"
<Ph**@parker246.freeserve.co.uk> wrote,
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
Jul 22 '05 #11

P: n/a
"Philip Parker" <Ph**@parker246.freeserve.co.uk> wrote in message news:<c8**********@news7.svr.pol.co.uk>...
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;

}
Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.