linux 底层文件访问(库函数)

本文提供了三个基本的系统调用示例:write用于输出字符串,read用于读取输入,open用于文件创建。这些示例展示了如何使用C语言进行简单的文件操作及输入输出交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

write系统调用

#include<unistd.h>
#include<stdlib.h>

int main()
{
        if( (write(1,"here\n",6) !=6))
        {
                write(2,"WRONG",5);
        }
        exit(0);
}



read系统调用

#include<unistd.h>
#include<stdlib.h>

int main()
{
        char buffer[128];
        int nread;

        nread = read(0,buffer,128);
        if(-1 == nread)
        {
                write(2,"read WRONG",10);
        }
        if( ( write(1,buffer,nread) )!= nread )
        {
                write(2,"A write error",13);
        }
        exit(0);
}

open 系统调用

#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>

int main()
{
        open("open.txt",O_CREAT,S_IRUSR|S_IXOTH);
        exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值