C语言实战(三)

本文介绍了在不同操作系统中进行控制台操作的方法,包括屏幕清除、光标移动、输入控制等,并提供了在Windows和Linux环境下实现这些功能的具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

 }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值