Menu

ANSI ESCAPE SEQUENCE

2005-05-19
2012-09-26
  • Nobody/Anonymous

    Hello!!

    I tried to use an ANSI escape sequence to clear the screen and what I got was this at the screen:

    <-[2J

    I used the comand:

    cout << '\033' << "[2J";

    So, I would like to know if the command above is right and if the output at the screen is normal.

    I use windows XP, so, Do I need to modify my config.sys to use the ANSI escape sequence ? I opened the config.sys and it was empty.

    See You

    Nosophorus

     
    • Nobody/Anonymous

      in Windows xp, you probably need to enable ANSI escape codes by loading
      ANSI.SYS. Here's some info, but you may need to modify it for XP etc,

      http://support.microsoft.com/default.aspx?scid=kb;en-us;Q101875

      I also had to use djgpp to compile my code to use the ansi escape sequence

      cout << "\033[0;31mhello \033[0;39mworld";

       
      • Anonymous

        Anonymous - 2006-10-22

        I am not sure why you dug up as 20 month old thread to tell us that!

        If you read the article carefully, you will see that it only applies to MS-DOS applications running on the Win32 16bit DOS subsystem. MinGW cannot produce MS-DOS programs, which is why you had to use djgpp. It is however a backward step, and on ethet may not be supported in future versions of Windows. 64 bit Windows certainly does not support a 16bit subsystem, only a 32bit subsystem.

        Clifford

         
        • Nobody/Anonymous

          Sorry about the dig up but for a program I am working on for uni I am required to use the ANSI escape sequence and everything. Thought I would post my findings and such.

           
          • Anonymous

            Anonymous - 2006-10-22

            Use a PC with two serial ports, connect the two ports together with a Null Modem cable, redirect I/O for your program to COM1 and open an ANSI terminal emulation program (such as HyperTerminal or TeraTem) on COM2. The whole set up will then behave just like the Unix platform I assume your Uni expect you to be using.

            Perhaps a mad idea, but at least then you will be able to use your compiler of choice rather than necessity. The problem is that few PC's these days have one let alone two serial ports. There are USB serial port adapters available or add-in cards, but now it is starting to cost money.

             
    • Nobody/Anonymous

      Sorry I can't help you with ANSI.sys. I can show you win32 console specific code to clear the screen:

      include <windows.h>

      void ClearScreen()
      {
      COORD coordHome = { 0, 0 };
      DWORD dwConSize, dwCharsWritten;
      CONSOLE_SCREEN_BUFFER_INFO csbi;

      HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE );
      GetConsoleScreenBufferInfo( hCon, &amp;csbi );
      dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
      FillConsoleOutputCharacter( hCon, TEXT(' '), dwConSize, coordHome, &amp;dwCharsWritten );
      SetConsoleCursorPosition( hCon, coordHome );
      

      };

       
    • Nobody/Anonymous

      What's wrong with system("CLS") to clear the screen?

       
      • Anonymous

        Anonymous - 2005-05-19

        Nothing, except that it has to invoke cmd.exe in order ot execute the CLS command, so it is much less efficient that the code presented above.

        Bear in mind that doing it in the manner described is not as much effors as it might seem. Once you have added this to your library of useful utilities (what, you don't have one?!), you simply re-use it.

        Clifford

         
    • Anonymous

      Anonymous - 2005-05-19

      The MS-DOS ansi.sys device driver is not supported in Windows NT / 2000 / XP.

      I have never found a satisfactory solution for this other that using the console API as described. The full range of console functions is described at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp

      If you are making extensive use of ANSI escape sequences then one kludgy possibility is to connect COM1 to COM2 via a null modem cable, and send the output to COM1, but view your application on COM2 using a terminal emulator. Unfortunately these days PCs often have only one or even no serial ports - and it'll win no prizes for speed! You'd have to be really desparate to try this!

      Clifford

       
    • Nobody/Anonymous

      Hello!!

      I'm very glad with your answers.Thank You!! :D

      But, I would like to know some independet platform way to clear the screen. I don't know if using "system("cls")" would be the most suitable to use, because when we use "system("")" we create one process just to clear the screen and stop another, and when the process created finishes, the stopped one continues. I need good performance and use "system("")" would not be the best or, at least, a good way to do it.

      But I thanks your answer. :D

      Nosophorus

       
      • Anonymous

        Anonymous - 2005-05-19

        There is no platform independent method. Because not all platforms employ the same console hardware.

        Your best bet is to encapsulate the code into a function, and implement the function on all required platforms. Put all your platform dependent code into a library, and the rest of your platform independent code can link to that.

        Clifford

         
    • Nobody/Anonymous

      Just a little observation:

      the "\n" isn't an ANSI Escape Sequence ??
      It works well here in my PC with windows XP. Or would be the ANSI Escape Sequence

       
      • Anonymous

        Anonymous - 2005-05-19

        Uh?

         
    • Nobody/Anonymous

      < Just a little observation: the "\n" isn't an ANSI Escape Sequence ?? It works well here in my PC with windows XP. Or would be the ANSI Escape Sequence >

      I guess it is...
      http://msdn.microsoft.com/library/en-us/vclang/html/_clang_Escape_Sequences.asp

      I made some assumptions when config.sys was mentioned. If someone gets the clear-screen to work with an escape sequence I'd be glad to hear it.

      While looking around I found:
      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/clearing_the_screen.asp
      Thinking this may be more technically correct I guess I'll be updating the ClearScreen( ) I used above.

      Also I can not comment on portability of "system("cls");" but I think it's not very, if at all. I suggest that users pick one or the other to suit their taste, and consider Cliff's comments.

       
      • Anonymous

        Anonymous - 2005-05-20

        Oh, now I see what he (she?) meant.

        He (and you) are confusing ANSI C character escape sequences with ANSI Video Terminal escape sequences.

        ANSI is an engineering standards body. It releases statnadrd on a vaiety of things. The C programming language, and Video Terminal standards are just two of these.

        The terms "escape sequence" in these two standards are not related in any other way other that they indicate that what follows is not literal but has a special interpretation.

        As I said, other that using a terminal emulator such as TeraTerm or HyperTerminal, ANSI escape sequences ar no supported in XP, the ANSI.SYS device driver has never been ported to 32bit code and does not work in XP.

        It is of course possible to write your own output routines to translate escape sequences into console API operations. You could even I suppose overload the ostream::operator<< function to take an new class that implements strings containing ANSI escape sequences, consequently you could write code that might look like:

        cout << termstring("<ESC>[2JNew screen") << endl ;

        or

        termstring display( "<ESC>[2JNew screen" ) ;
        cout << display << endl ;

        Which would clear the screen and display New screen at the top left. Just an idea, somewhat pointless I suspect, once you have created a library of console manipulation functions to meet your requirements, you mat as well use them directly.

        Clifford

         
    • Nobody/Anonymous

      < He (and you) are confusing ANSI C character escape sequences with ANSI Video Terminal escape sequences. >

      I think you are just catching up now and prefer to blame another.

       
      • Anonymous

        Anonymous - 2005-05-20

        I am not sure what you mean. My orignial "Uh?" response was because the statement

        "the "\n" isn't an ANSI Escape Sequence ?? "

        made no sense unless read with the correct intonation, which is hard to convey in writing. It seems to me that what he must have meant was:

        "Is "\n" not an ANSI Escape Sequence?"

        The first is a statment (albeit with two question marks added), the second is actually a question - to which the answer in this context is no.

        The only thing I am catching up on is the original ambiguous post. I am not sure who you think I am blaming or for what. I was merely attempting to clarify any confusion there may have been - (I thought).

        I am not really sure who's speaking since every other respndant is an anonymous user and not signing their posts, but if it were you who posted the left-field '\n' remark, they I would suggest that it is you who are catching up and seeking someone to blame.

        Clifford

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.