Linux学习:对文件的创建及读取操作

目录


 

1、创建文件并读取该文件的内容

用read()函数来读取文件,read函数原型:

         #include <unistd.h>   

       ssize_t read(int fd, void *buf, size_t count);

参数表:

int fd指向open的文件描述符
void *buf将读取的数据放在buf
size_t count读取到的数据字节数        
返回值

返回读取到的字节数,什么都读取不到返回0,失败返回-1      

demo:

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

int main()
{

        int fd;
        char *buf = "nihao";
        fd = open("./file1",O_RDWR);//打开了一个当前路径下一个叫file的文件
        if(fd==-1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);//返回值若为-1打开失败,并且创建一个叫file的文件,0600是可读可写的权限
                if(fd>0){
                printf("creat file1 success\n");
}
        }
        printf("open success : fd = %d\n",fd);
        int n_write = write(fd,buf,strlen(buf));往open的file文件写入buf的内容
        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }
       
        

        char *readBuf;//定义一个新的缓存
        readBuf = (char *)malloc(sizeof(char)*n_write+1);为缓存开辟空间,大小为写入file的字节数+1
        int n_read = read(fd,readBuf,n_write);//读取file的内容,读的大小为write进file的字节数并且将数据读到readbuf缓存

        printf("read %d ,context:%s\n",n_read,readBuf);//将读到的字节数和内容输出


        close(fd);//open后关闭文件
        return 0;

2、创建文件时设置文件权限

        0600是创建文件时给该文间的权限参数,通过ls -l指令可以查看当前路径下文件的权限:

r4可读
w

2

可写
x1可执行

 

 0600代表文件所有者有可读可写的权限

运行结果:

    

        执行命令后路径下多了一个叫file1的文件打开内容是write写入的buf,但是打印的read的字节数和内容全部丢失,出现一个bug,出现这个bug是因为在write数据到新创建的文件的时候,文件的光标停留在了最后一位,之后read的数据都是从光标位置开始的,光标之后没有内容所以没有任何数据。

3、解决因为光标位置导致读取失败问题

一般解决方法有两种:

1、read读取文件之前先将文件给close()关闭,然后重新打开

下半部分改为:

运行结果:read到了数据

 2、对光标进行操作

用lseek实现:

          #include <sys/types.h>
         #include <unistd.h>

         off_t lseek(int fd, off_t offset, int whence);

         将文件读写指针相对whence移动offset个字节,whence有三个参数选择

whence:

SEEK_SET光标位置指向文件头
SEEK_CUR光标位置指向文件当前位置
SEEK_END光标指向文件结尾巴
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{

        int fd;
        char *buf = "zhuyueshuai";
        fd = open("./file1",O_RDWR);
        if(fd==-1){
                printf("open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd>0){
                printf("creat file1 success\n");
}
        }
        printf("open success : fd = %d\n",fd);
        int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write %d byte to file\n",n_write);
        }
//      close(fd);
//      fd = open("./file1",O_RDWR);
        lseek(fd,0,SEEK_SET);//光标选择文件头的位置,偏移值为0
        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write+1);
        int n_read = read(fd,readBuf,n_write);

        printf("read %d ,context:%s\n",n_read,readBuf);



        return 0;
}
~                       

         运行结果与上方一致,如果用的不是文件头的位置,用的是结尾或者当前位置的话,需要往前偏移,则第二个参数偏移值为负数就可以往前偏移,正数时往后偏移。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值