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

4 篇文章 0 订阅

 

 

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

int fd = creat(路径,权限);

ssize_t  fd2 = write(句柄,数据,数据长度);

ssize_t  fd2 = write(句柄,数据,数据长度);

 

int main(void){
   int fd = 100; #句柄
   char *path = "./CreatedFile";#创建文件路径

   fd = creat(path,0777);#文件路径和文件创建权限
   if(fd != -1){
   printf("Creat success!%s is %d\n",path,fd);#成功之心这里
   }
   else{
        printf("CreatError!fd = %d\n",fd); #失败打印这里
   
   }


return 0;
}


#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>

int main(void){
    int fd = 100;
    ssize_t fd2;
    char *path = "./hello.c";
    char *things = "#include <stdio.h>";

    fd = open(path,O_RDWR|O_CREAT);
    if(fd != -1){
        printf("Open Succ!\n"); 
    }
    else{
        perror("Open Fail!\n");
    }

    fd2 = write(fd,things,strlen(things));
    if(fd2 != -1){
        printf("\nWrite Succ!\n"); 
    }
    else{
        printf("\nWrite Failed\n");
    
    }
    close(fd);

    

    


return 0;
}

ssize_t rd = read(句柄,存储数据的头指针,要存储数据的长度);

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

#define MAX 1000

int main(void){
    int a;
    ssize_t rd = 999;
    int fd = 999;
    char *path = "./hello.c";
    char read_buf[MAX];

    fd = open(path,O_RDWR);
    if(fd != -1){
        printf("Open success!\n"); 
    }
    else{
        perror("Open failed!\n"); 
        return -1;
    
    }

    rd = read(fd,read_buf,MAX);
    if(rd != -1){
       for(a = 0; read_buf[a] != 'X'; a++){
        printf("%c",read_buf[a]); 
       } 
    
        printf("\n"); 
    }
    else{
    
        printf("read failed!\n"); 
        return -1; 
    }
    
    close(fd);  
return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值