简体   繁体   中英

Is clrscr(); a function in C++?

I've looked everywhere for this function and cannot find the header files to make this work. It says clrscr() undeclared which brings me to the question. Is clrscr(); a function in C++?

It used to be a function in <conio.h>, in old Borland C compilers.

It's not a C++ standard function.

And before someone posts the usual "please email me the conio.h file" request, can I point out that this ancient Borland header file only contained the declaration of the function. You would also need the supporting Borland library, which will not be compatible with any modern C++ compilation system.

As mentioned before, clrscr() is from turbo c++, inside conio.h

For all intents and purposes, conio.h is "non standard", and as such should be probably avoided.

I tend to use the precompiler to choose what to use for a simple clear screen, and just call the operating system's clear program.... it's smart enough to know how "tall" the screen is.

// somewhere in the program
#define WINDOWS 1

void console_clear_screen() {
  #ifdef WINDOWS
  system("cls");
  #endif
  #ifdef LINUX
  system("clear");
  #endif
}

In windows, you may want to look at the windows.h, You can interact with the windows console directly using a "handle", often noted in code as an hWin.

In linux, i've had good luck with curses/ncurses, although it is a little confusing at first.

update Calling system programs (clear.exe?)is a potential security risk - if someone is able to hijack the system call somehow thru an alternate avenue, they can force your program to do strange things. My recommendation is to dig into your platform's console api to get these things done.

你必须包含此函数的头文件

#include <conio.h>

you can use the system cls command to clear the output screen..

clrscr() is from turbo c++, inside conio.h and conio.h is "non standard", and as such should be probably avoided. example

    #include<windows.h>
    main()
    {
    some code....;
    system("cls");
    some more code;
    }

its tested and works.. i use dev c++ with mingw compiler.. :)

A web search says the header file you want is 'conio.h' - I haven't tried it out, so no guarantees. Its existence may also depend on what platform you are compiling against.

On Linux I always use:

void clrscr(void)
{
   fprintf(stdout, "\033[2J"); // clean screen
   fprintf(stdout, "\033[1;1H"); // move cursor to the first line
}

On Unix-like systems you can use VT100 escape codes.

std::cout << "\033[2J" << std::flush;

See http://www.termsys.demon.co.uk/vtansi.htm

The easiest way to clear the screen in real C++ is to just send out a bunch of blank lines. Of course this is assuming that stdout is directed at the screen and not a file:

for (int i = 0; i < 80; ++i)
     cout << "\n";
cout << endl;

I used to do borland too.

Investigating curses is a good idea. It works on many unix platforms.

You might take a look at nconio at source forge.

This looks promising as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM