我希望能够清除屏幕上所有的文本内容。 我已经尝试使用:
#include <stdlib.h>
sys(clr);
提前致谢! 我使用的是OS X 10.6.8操作系统。抱歉有所混淆!
curses.h
是什么以及为什么他应该查看curses.h
,那可能会更好些。 - Sam I am says Reinstate Monica清除屏幕最好的方法是通过调用stdlib.h中的system(const char *command)
来调用shell:
system("clear"); //*nix
或者
system("cls"); //windows
再说一遍,尽量减少对调用系统/环境的函数的依赖,因为它们可能会导致各种未定义的行为。
Windows:
system("cls"); // missing 's' has been replaced
Unix:
system("clear");
void clearscr(void)
{
#ifdef _WIN32
system("cls");
#elif defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
system("clear");
//add some other OSes here if needed
#else
#error "OS not supported."
//you can also throw an exception indicating the function can't be used
#endif
}
注意检查unix是非常广泛的。这也应该能够检测到您正在使用的OS X。
#include <stdio.h>
void clearscr ( void )
{
for ( int i = 0; i < 50; i++ ) // 50 is arbitrary
printf("\n");
}
cout <<
那样的操作符重载在C中是不允许的。我关于它实际上没有清除屏幕的观点是,你的光标留在控制台底部而不是顶部。这不仅仅是光标寻址的问题,它是一个非常明显的外观差异。当你清除屏幕并开始输入时,你期望它出现在第一行,而不是最后一行。在大多数情况下,它可能在功能上是相同的,但对用户来说看起来和感觉都非常不同。 - brianmearns
clrscr();
函数在C语言中不能使用了吗?我用过很多次啊。 - Sid