1、清屏函数clrscr是TC特有的,其它的C语言环境没有这个函数,也就没有头文件包含这个函数。
建议使用 system("cls");来取代clrscr();比较通用,兼容性好一点。
system()函数在#include <stdlib.h>里面。
/*
printf("| The program will show : |");
printf("/r"); 回车
*/
2、在VC中conio.h头文件中没有gotoxy(x,y)
要自己去定义.
#include "windows.h"
void gotoxy(int x,int y)
{
COORD c;
c.X=x-1;
c.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
3、获取光标位置函数:
BOOL GetConsoleScreenBufferInfo( HANDLE hConsoleOutput, PCONSOLE_SCREEN_BUFFER_INFO lpConsoleScreenBufferInfo );
lpConsoleScreenBufferInfo->dwCursorPosition.X返回光标位置x坐标
lpConsoleScreenBufferInfo->dwCursorPosition.Y返回光标位置y坐标
设置光标函数:
BOOL SetConsoleCursorPosition( HANDLE hConsoleOutput, COORD dwCursorPosition );
dwCursorPosition.X 光标位置x坐标
dwCursorPosition.Y 光标位置y坐标
注意:包含文件windows.h
示例:
#include <windows.h>
#include <stdio.h>
int main()
{
CONSOLE_SCREEN_BUFFER_INFO csbinfo;
COORD CursorPos;
CursorPos.X=10;
CursorPos.Y=10;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&csbinfo);
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),CursorPos);
printf("123456/n");
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),csbinfo.dwCursorPosition);
return 0;
}
4、
#include <dos.h>
#include <system.h> sleep() 秒 --------linux #include "Windows.h" #include "Winbase.h" Sleep()毫秒 ------------MFC Vc6没有delay() sleep()
a> MFC中的Sleep函数原型为:
void Sleep( DWORD dwMilliseconds );
b>linux下的sleep函数原型为:
unsigned int sleep(unsigned int seconds);
MFC中的是微秒,linux下的是秒。linux下用微秒的线程休眠函数是:
void usleep(unsigned long usec); int usleep(unsigned long usec); /* SUSv2 */
或者用select函数+timeval结构也可以(最多精确到微秒),
或者用pselect函数+timespec(可以精确到纳秒,足够精确了!)
5、在LINUX下这一切都非常简单,无需任何函数,printf即可完成。
清除屏幕:printf("%c[2J",27);
光标跳转到屏幕x(1~25)行、y(1~80)列:printf("%c[%d;%dH",27,x,y);
getch这样的函数太垃圾了,在LINUX下可以控制一切输入函数是否会显,包括scanf、gets等,方法是: 禁止输入时回显:system("stty -echo"); 允许输入时回显:system("stty echo");
6、windows和linux里,x,y坐标互调,不一致;
printf("%c[%d;%dH",27,p,y);
printf("****"); //无/n,竟打印不出×××。
7、getch()是个不可移植的函数,可以自己模拟出来一个。测试代码如下:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
int getch(void);
int main(void)
{
char ch;
printf("Input a char:");
fflush(stdin);
ch = getch();
printf("/nYou input a %c/n", ch);
return 0;
}
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;
}