//注:以下结果在linux下编译通过
//open()函数:
//头文件:#include <unistd.h> #include <fcntl.h>
//原型
int open(const char * pathname, int flags);
int open(const char * pathname, int flags, mode_t mode);
//说明:pathname时文件名,flags是打开方式,mode是新建文件时的权限标识(详见http://c.biancheng.net/cpp/html/238.html)
//这里比较有意思的是open()的返回值是int,而非FILE*,这里的int类型的返回值,通常用fd表示(FileDescriptor)
//每个进程都有个 映射文件物理地址的表格(0:标准输入 1:标准输出 2:标准错误 3、4、5....就映射你打开过文件的地址)
//若以整形输出下面的程序的fd,可得3.
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
main()
{
int fd, size;
char s[] = "Linux Programmer!\n", buffer[80];
fd = open("opentest.txt", O_WRONLY|O_CREAT);
write(fd, s, sizeof(s));
close(fd);
fd = open("opentest.txt", O_RDONLY);
size = read(fd, buffer, sizeof(buffer));
close(fd);
printf("%s", buffer);
}
//result output:
sxy@ubuntu:~/Documents/linux_learning/test$ ./test.o
Linux Programmer!
open()函数解析
最新推荐文章于 2024-08-31 15:50:11 发布