day25.creat创建操作

本文通过一个具体的C语言程序示例介绍了如何使用Linux系统调用creat函数来创建文件。示例程序尝试打开或创建几个文件,并展示了不同文件权限设置及打开方式的影响。

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

第25节:creat创建操作

1、编写运行编译creat函数例程:
root@ubuntu:/home/linuxsystemcode/create# cat creat.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
main(){
        int fd;
        char *leds = "/dev/leds";
        char *test1 = "/bin/test1";
        char *test2 = "/bin/test2";

        char *test3 = "/bin/test3";

        if((fd = open(leds,O_RDWR|O_NOCTTY|O_NDELAY))<0){
                printf("open %s failed!\n",leds);
        }

        printf("%s fd is %d\n",leds,fd);

        if((fd = open(test1,O_RDWR,0777))<0){
                printf("open %s failed!\n",test1);
        }

        printf("%s fd is %d\n",test1,fd);

        if((fd = open(test2,O_RDWR|O_CREAT,0777))<0){
                printf("open %s failed!\n",test2);
        }

        printf("%s fd is %d\n",test2,fd);

        fd = creat(test3,0777);
        if(fd == -1){
                printf("%s fd failed,fd is %d\n",test3);
        }
        else{
                printf("creat %s fd is succesed! fd is %d\n",test3,fd);
        }
}

2、进行文件编译
root@ubuntu:/home/linuxsystemcode/create# arm-none-linux-gnueabi-gcc -o creat creat.c
root@ubuntu:/home/linuxsystemcode/create# ls
creat  creat.c

3、将文件拷贝到TF卡上,并传入开发板上运行结果

[root@iTOP-4412]# ./creat                                                                                          
[ 1388.047982] LEDS_CTL DEBUG:Device Opened Success!
[ 1388.051580] LEDS_CTL DEBUG:Device Closed Success!
/dev/leds fd is 3
open /bin/test1 failed!
/bin/test1 fd is -1
/bin/test2 fd is 4
creat /bin/test3 fd is succesed! fd is 5



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随行之旅

python国产化自动化

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值