1、创建读取函数文件,文件名称testopen.c 如下:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
int main() {
int fd;
int rc;
char buf[512] = {0};
fd = open("read.txt",O_RDONLY);
printf("fd:%d\n",fd);
while(0 < (rc = read(fd,buf,512))){
printf("%s\n",buf);
}
printf("read ok\n");
close(fd);
return 0;
}
2、创建需要读取的文件 read.txt ,并输入内容。
3、
gcc testopen.c -o read
./read