Aug 072008
 

gotoxy! what’s that?

gotoxy was one of my favorite functions in turbo c++, it moves the input cursor in a console application/MS-DOS application from one location to another.

So is there an equivalent function in a windows console application which does the same? Yes, it’s called SetConsoleCursorPosition.

Sample code

As always with me here is a function which does this!

BOOL gotoxy( const WORD x, const WORD y )
{
   COORD xy;
   xy.X = x;
   xy.Y = y;
   return SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), xy );
}
int main()
{
   if( !gotoxy( 10, 10 ))
   {
      std::cout < < "gotoxy failed :(, seems like coords are too big\n";       return 1;    }    return 0; }[/sourcecode] Result of calling "gotoxy", see how the pause message has shifted down and to the right.

Screenshot for gotoxy

Note that our gotoxy can fail if x or y is too big. A post from me will follow soon based on this function. It’s going to be about displaying a spining stick at the end of a progress message. You might have seen it if you have installed Linux.

Sep 202007
 

Remember good old unix/linux command called touch, so simple but very useful, it changes the modified and last access time of a file. Ever felt the need for the same in windows, well I did sometime back.

There is a function in imagehlp.dll which is called TouchFileTimes. This is how we use it…

// Include file
#include "Imagehlp.h"

// Link to this library
#pragma comment( lib, "ImageHlp.lib" )

int main( int argc, char **argv )
{
    HANDLE hFile = CreateFile( "C://SomeFile.ext", GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
    ASSERT( hFile );

    // If second parameter is NULL then system time will be used, or the given date time
    TouchFileTimes( hFile, 0 );
    CloseHandle( hFile );

    return 0;
}// End main

Output:

Right click and open properties for the file and see what’s the last modified time,
it will be the current system time.