Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

the function clrscr() with visual c++

Status
Not open for further replies.

Faustino29

Programmer
Mar 24, 2001
5
BE
Heps! I have a big problem...I usually used the function
clrscr() in the Borland C 5.0 to clear the screen and I'm using Visual C++ 5.0 and also 6.0 and it doesn't want to compile the program and run it!! I don't find the right instruction to use with the Visual C++ 5.0...
Could help me please??

Thanks!
 
I must admit that I'm unfamiliar with Borland and I don't know exactly what clrscr() did. But I'm guessing that this function was used to clear the screen when writing a command line application.

When writing a command line app in VC++, one way (maybe not the best way??) to clear the screen is to use the system function:

system( "cls" );

This function executes a DOS command for you. Note that you'll need to include <Process.h> in order for the function call to compile.

You can read more about the system function at

 
As you're discovering, clrscr() is a compiler-specific extension which introduces portability issues. In fact, clearing the screen period is inherently non-portable in C, so you may want to think about whether or not you really *need* to clear the screen in the first place.

Using system(&quot;CLS&quot;) is not such a great idea. One reason is because the system() function is slooooow. But more importantly, your application creates a security hole because it makes the shaky assumption that the command &quot;CLS&quot; will actually clear the screen. IOW, there's nothing that your application can do to be sure that &quot;CLS&quot; hasn't been replaced by a maliscious program that deletes all of your files instead of clearing the screen. Given alternatives, system() should really be your last choice.

Here is a link to what msdn has to say about doing it in VC++. It actually suggests using system(&quot;CLS&quot;) (and uses void main() in the examples!) and gives another better solution that consists of a dozen or more lines of code:


Russ
bobbitts@hotmail.com
 
Hey there ,
&quot;CLS&quot; is not a program.. Its interpretted by the
shell. As such, no disk program corresponds to CLS ?
Am i right ? So, use it..Why not ?
Do not rejoice that ur code works.
it might be a special case of an error :-(
 
Yes, you're right. So, please ignore the 2nd part of my reasoning against system(&quot;CLS&quot;). My apologies for the misinformation.

However, the 1st part still holds in that using system() can be costly in terms of how long it takes to execute. Another thing to consider is that users are often annoyed at programs that clear the screen, so you may want to take that into consideration before deciding that clearing the screen is a good idea however you do it.

Russ
bobbitts@hotmail.com
 
try this function:


#include <windows.h>
#include <conio.h>

int clrscr()
{

HANDLE hndl = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hndl, &csbi);
DWORD written;
DWORD N = csbi.dwSize.X * csbi.dwCursorPosition.Y +
csbi.dwCursorPosition.X + 1;
COORD curhome = {0,0};

FillConsoleOutputCharacter(hndl, ' ', N, curhome, &written);
csbi.srWindow.Bottom -= csbi.srWindow.Top;
csbi.srWindow.Top = 0;
SetConsoleWindowInfo(hndl, TRUE, &csbi.srWindow);
SetConsoleCursorPosition(hndl, curhome);

return 0;
}
 
This is in ref. to the question posed by Sarnath - How cls or for that method any other DOS command can be used for malfunctioning.
How can u be so sure that there are no TSRs written on the disk to save u from what rbobbitts said in his first post.

And even there are more easy way to bug up this command. If you can recall the DOS fundamentals DOS prioritizes execution as:
*** Macros,Internal Commands, .EXE file in same directory, External command, ......
so I do feel that using cls may perform a malicious operation on your computer by posing a critical threat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top