I am trying to mess with the file position indicator and write over stuff that is already on screen.
#include <stdio.h>
int main ()
{
fpos_t position;
fgetpos (stdout, &position);
fputs ("That is a sample",stdout);
fsetpos (stdout, &position);
fputs ("This",stdout);
return 0;
}
I want this "This is a sample". I got similar code right off of cplusplus.com the only difference is that they use an actual file and not stdout. Is there some special exception for stdout of which I am unaware.
I thought I could treat stdout like a file. For some reason I am getting this as output: That is a sampleThisPress any key to continue . . .
I would really like to know why. This guy even asked the same question on with no response on cplusplus.com
I know about fseek and lseek and I might use those instead if they work but regardless I want to know why the above does not work. If you have a better way of doing this I am open to suggestions but I still want to know what I am doing wrong here. Thank you in advance.
If what you are trying to achieve is to modify the output to a screen, you may want to look at ncurses (or something similar).
Or perhaps, if you just want something like this (a progress bar that shows how much "part2 is of the "total" work that is done so far):
....
cout << part * 100 / total << "% done\r"; cout.flush();
....
The \r is "carriage return", and will move the cursor back to the start of the line, without moving down.
Stdout doesn't have a concept of file positions when pointing at tty style devices, think of the old days of tty modems etc and once a character is sent it's sent. You may be able to send a sequence of characters to reposition the cursor after the event and overwrite text on the screen, but how you do that is dependent on the output device.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With