open()函数解析

//注:以下结果在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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值