ARM学习笔记1-文件IO操作

4 篇文章 0 订阅

                                   ARM学习笔记1-文件IO操作

/*加载头文件*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(void){
    int fd;//加载句柄;
    char *leds = "/dev/leds";//加载文件路径
    char *test1 = "./test1";//在当前文件夹下创建test1文件
    char *test2 = "./test2";//在当前文件夹下创建test2文件

    /*如果打开/dev/leds <0,则打印OPen %s failed,否则打印成功返回的句柄*/
    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);
    
    /*打开名为test1的文件,并给予777权限,如果fd < 0,则表示打开失败,打印失败返回句柄;*/
    if((fd = open(test1,O_RDONLY,0777)) < 0) printf("read %s is Failed!\n",test1);
    printf("%s fd is %d\n",test1,fd);

    /*打开名为test2的文件,并给予777权限,如果没有文件,则创建它,失败返回失败,成功返回句柄;*/
    if((fd = open(test2,O_RDONLY|O_CREAT,0777)) < 0) printf("read %s is Failed!\n",test1);
    printf("%s fd is %d\n",test2,fd);
    return 0;
}

打开文件open函数
• int open(const char *path, int oflags);
• int open(const char *path, int oflags,mode_t mode);

– 参数path表示:路径名或者文件名。路径名为绝对路径名。
– 参数oflags表示:打开文件所采取的动作

-mode表示:设置创建文件的权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值