如何实现“Press any key to continue...”


---------------------------------------------------------------------------

Windows 下:

#include <stdio.h>
#include <conio.h> /* For getch(), non-portable. */

int main(int argc, char *argv[])
{
printf("Press ENTER to continue...");
getch();

return 0;
}

---------------------------------------------------------------------------

Linux 下:

法一:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

/* Please reference the manpage (man termios) for more details. */

int mygetch(void)
{
int ch;
struct termios oldt;
struct termios newt;

tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
/*
* Unset the CANONICAL mode, in which input is available immediately,
* and the ECHO mode.
*/
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);

return ch;
}

int main(int argc, char * argv[])
{
printf("Press any key to continue...\n");
mygetch();

return 0;
}

法二:(会清屏,不如法一)

#include <ncurses.h>
#include <unistd.h>

int main(int argc, char * argv[])
{
initscr();
mvprintw(5, 5, "Press any key to continue...");
getch();
endwin();

return 0;
}

---------------------------------------------------------------------------


参考:
1. http://www.timectrl.net/bbs/viewthread.php?tid=54
2. http://www.linuxsir.org/bbs/printthread.php?p=232994

你可能感兴趣的:(linux,PHP,windows,.net,bbs)