qfedu-linux-advanced-level/day2/d1.c

21 lines
679 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
// 修改标准的输入设备的标识为非阻塞
int flags = fcntl(STDIN_FILENO, F_GETFL);
flags |= O_NONBLOCK; // 添加非阻塞标识,可以注释这一行来测试不同的效果
fcntl(STDIN_FILENO, F_SETFL, flags);
char buf[32] = "";
printf("准备读取数据......\n");
// read 默认是阻塞的,如果没有数据,会一直等待
// 接收到一个回车read就会返回
ssize_t len = read(STDIN_FILENO, buf, sizeof(buf));
printf("读取的内容为:%s\n", buf);
return 0;
}