Quick C Question!
Lo All,
Just a really quick question...
How do you clear the screen in C?
Ive tried the system("cls") one but it takes like 2 secs to clear the screen, which sucks
Also people have been tellin about the clrscr() function but the compiler just tells me its a undeclared identifier...So do I need to use a different header file or somin, if so which one?
Thanks in Advance...
You can use clrscr() in DOS, yes, and in order to use it you gotta include "conio.h" without the quotes of course...
Im programming in windows 95, using MSVC++ 5.0 and Ive tried the clrscr() with the conio.h header and it still doesnt work any other ideas?
This is a very dirty solution, but it''s working. I hope this code don''t shame on me, because I''m a game programmer for nine years.
void main (void)
{
char temp[80 * 25] ;
memset (temp, 0, 80 * 25) ;
fwrite (temp, 80 * 25, 1, stdout) ;
}
void main (void)
{
char temp[80 * 25] ;
memset (temp, 0, 80 * 25) ;
fwrite (temp, 80 * 25, 1, stdout) ;
}
So you are making a Win32 Console App and not a DOS app.. okay... that means that you can''t use the clrscr() function since it doesn''t exist (unless there''s a library out there that would have it)... Here is some code that you can use in order to clear the screen in a Win32 Console Application....
#include
#include
int main(void)
{
char buf[10];
for (int i = 1; i <= 24; i++)
{
sprintf(buf, "%d", i);
printf("Line #%s\n", buf);
}
getch();
// The following two lines basically clear the screen
// There are other ways to do the same thing out there // but this is the easiest
for (i = 0; i < 24; i++)
printf("\n");
getch();
return 0;
}
#include
#include
int main(void)
{
char buf[10];
for (int i = 1; i <= 24; i++)
{
sprintf(buf, "%d", i);
printf("Line #%s\n", buf);
}
getch();
// The following two lines basically clear the screen
// There are other ways to do the same thing out there // but this is the easiest
for (i = 0; i < 24; i++)
printf("\n");
getch();
return 0;
}
Nb. Using printf also has the added disadvantage of pushing your cursor to the bottom of the screen...but if you''re drawing the entire screen again, shouldnt be too much of a problem.
Also IMHO, this is a bad way of doing it. Doesn''t window have any console manipulation libs? The equiv of curses under unix? *shakes head* sorry; cant be much help. Never done that sort of thing under windows.
Also IMHO, this is a bad way of doing it. Doesn''t window have any console manipulation libs? The equiv of curses under unix? *shakes head* sorry; cant be much help. Never done that sort of thing under windows.
Way to clear the screen in MSVC++
HANDLE output;DWORD numchars;COORD startpos = {0,0};FillConsoleOutputCharacter( hOutput, '' '', 0xffff, startpos, &numchars);gotoxy (0,0);
June 12, 2000 01:12 AM
In VC++, there are functions to move the cursor around the screen. Here is a function that does this:
int SetCursorPosition(int x, int y)
{
HANDLE screen_buffer_handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(screen_buffer_handle, coord);
}
And of course you can wrap the clearscreen function and cursormove provided by jonny all into one:
int ClearScreen()
{
char temp[80 * 25] ;
memset (temp, 0, 80 * 25) ;
fwrite (temp, 80 * 25, 1, stdout) ;
SetCursorPosition(0, 0);
}
You have to include windows.h, though. I don''t really know how fast/slow it is.
int SetCursorPosition(int x, int y)
{
HANDLE screen_buffer_handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(screen_buffer_handle, coord);
}
And of course you can wrap the clearscreen function and cursormove provided by jonny all into one:
int ClearScreen()
{
char temp[80 * 25] ;
memset (temp, 0, 80 * 25) ;
fwrite (temp, 80 * 25, 1, stdout) ;
SetCursorPosition(0, 0);
}
You have to include windows.h, though. I don''t really know how fast/slow it is.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement