linux环境C编程与windows的不同

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) 这个是关闭缓冲的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值