今天在看c++时发现了这样一个问题:
在WINDOWS中,可以用这样写来实现程序的暂停
#include
int main()
{
cout << "Hello world!!" << endl;
system("pause");
}
可是在LINUX中却不能做到,呵呵,这却实难到我了。
我上网查了一下,有牛人在/usr/include下加了头文件conio.h
#include
#include
#include
#include
int getch(void)
{
int c=0;
struct termios org_opts, new_opts;
int res=0;
/*---- store old settings ----*/
res=tcgetattr(STDIN_FILENO, &org_opts);
assert(res==0);
/*---- set new terminal parms ----*/
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
c=getchar();
/*---- restore old settings ----*/
res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res==0);
return c;
}
但是我在应用时,没有成功!
呵呵,C的到是有很多方法 http://blog.chinaunix.net/u2/67401/showart.php?id=576410
我这儿还有一个代码实现了:
#include
#include
#include
#include
int getch();
void press_key();
int main()
{
printf("Hello world!\n");
press_key();
return 0;
}
void press_key()
{
printf("Press any key to continue...\n");
getch();
}
int getch()
{
struct termios tm,tm_old;
int fd = STDIN_FILENO,c;
if (tcgetattr(fd, &tm) < 0)
{
return -1;
}
tm_old = tm;
cfmakeraw(&tm);
if (tcsetattr(fd,TCSANOW, &tm) < 0)
{
return -1;
}
c = fgetc(stdin);
if (tcsetattr(fd,TCSANOW,&tm_old) < 0)
{
return -1;
}
return c;
}
呵呵,多多发现,多多学习吧!!