Linux文件读取操作 1 -重新打开 2-把光标移头再读

#include <sys/types.h>
  2 #include <sys/stat.h>
  3 #include <fcntl.h>
  4 #include <stdio.h>
  5 #include <unistd.h>
  6 #include <stdlib.h>
  7 #include <string.h>



int main(){
  9  int fd;
 10 char *buf="pengyuandan snail\n";
 11  fd=open("./file1",O_RDWR);

 14  if(fd==-1){
 15      printf("open file  failed\n");
 16      fd=open("./file1",O_RDWR|O_CREAT,0600);
 17      if(fd>0){
 18         printf("creat file success!\n");
 19          }
 20    }
 21 
 22 printf("open success fd=%d\n",fd);
 23   int n_write=write(fd,buf,strlen(buf));
 24    if(n_write!=-1){
 25        printf("write %d",n_write);
 26        }
 27 //读取之后再进行重新打开fd
 28    close(fd);
 29  fd=open("./file1",O_RDWR);
 30 
       char*readbuf;
 32    readbuf=(char*)malloc(sizeof(char)*n_write+1);
 33 
 34    int n_read=read(fd,readbuf,n_write);
 35    printf("read=%d,context:%s\n",n_read,readbuf);
 36 
 37    close(fd);

    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值