help printing to the same pos with c++

Started by
4 comments, last by Fruny 19 years, 6 months ago
I am trying to print to the same postion every time example i want to print 1 then print 2 were one is does any one know how I would do that in c++ under dos? thanks
Advertisement
You'll find more information here.

You'll have to get the Standard Output Handle, and then you can use it to position the Cursor (that blinking thingee).


An easy example that should work:
#include <windows.h>int main(){    HANDLE hOut;    COORD co;        hOut = GetStdHandle( STD_OUTPUT_HANDLE );    co.X = co.Y = 5;            if( hOut != INVALID_HANDLE_VALUE )    {        SetConsoleCursorPosition( hOut, co );        cout << "Hello!";    }        return 0;}
this is how I did it

std::cout<< y<<"%";
std::cout<<"\b";std::cout<<"\b";std::cout<<"\b";std::cout<<"\b";

is there a way to make the cursor disaper tell I want it?
See my previous post (I edited it) for an example on how to place the cursor.

'\b' means backspace. '\r' goes back to the beginning of the line (but doesn't erase anything while going back).
ARe you talking about simple text file processing, where you want to overwrite what you've previously placed?

The easiest way would probably be to reset your filestream pointer with seekg(). If you are going back one place, then you are moving backwards by one byte (one char...assuming non-unicode). So you can do something like this:

#include <fstream>int main(){ofstream outfile;outfile.open("Sample.txt")long pis;  //position in stream.  Used to indicate where you are//write the first characteroutfile << "2";pis = outfile.tellp();  //where you currently are in the streamoutfile.seek(pis-1);    //move backwards one characteroutfile << "1";  //should overwrite the 2outfile.close();return 0;}


When you open the Sample.txt file, all you should see is "1".
The world has achieved brilliance without wisdom, power without conscience. Ours is a world of nuclear giants and ethical infants. We know more about war than we know about peace, more about killing than we know about living. We have grasped the mystery of the atom and rejected the Sermon on the Mount." - General Omar Bradley
Quote:Original post by kingpinzs
does any one know how I would do that in c++ under dos?


Under DOS, you load the ANSI.SYS driver and use the VT control codes.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement