linux环境C编程与windows的不同

来源:互联网 发布:java如何设置时区 编辑:程序博客网 时间:2024/05/05 06:28

1.system("pause")不能使用,可自己写一个mypause()函数


#include <stdio.h>#include <stdio_ext.h>void mypause(){    char ch;    printf("Press Enter to continue...\n");    __fpurge(stdin);    while((ch = getchar())!='\n' && ch != EOF)        ;}


2.system("cls")改用system("clear")


3.不能使用getch不回显字符的函数,只能自己编写函数设置终端参数来实现

#include <stdio.h>#include <string.h>#include <termios.h>#include <unistd.h>#include <assert.h>char getch(int len_max){    char ch;    struct termios old_attr, new_attr;    int len = 0;    int res = 0;    if(len < len_max)    {        res = tcgetattr(STDIN_FILENO, &old_attr);        assert(res == 0);        memcpy(&new_attr, &old_attr, sizeof(old_attr));        new_attr.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE);        res = tcsetattr(STDIN_FILENO, TCSANOW, &new_attr);        ch = getchar();        ++len;        res = tcsetattr(STDIN_FILENO, TCSANOW, &old_attr);        assert(res == 0);        return ch;    }    else    {        printf("Your in put must less than %d", len_max);        return -1;    }}


4.getchar() 回车键 识别为'\n',值为 10;退格键Backspace识别为delete,值为127


5.windows下目录路径用的是反斜杠 \,linux下是正斜杠 /

6.windows下中文是GBK编码, linux下是utf-8

7.windows下文本文件行尾是\r\n, linux下只有\n

8.fflush不能用于清空stdin,换成__fpurge(stdin),也不是setbuf(stdin, NULL) 这个是关闭缓冲的

0 0
原创粉丝点击