Linux中关于文件的IO(文件的创建、打开、写入、输出)

Linux文件IO操作:创建、打开、写入与追加
本文介绍了在Linux中如何使用系统调用来进行文件操作,包括使用open函数创建和打开文件,设置不同的权限标志如O_CREAT和O_RDWR。接着展示了如何读取(read)和写入(write)文件内容,以及如何使用lseek进行文件指针的移动来实现追加写入。示例代码详细演示了这些过程。

Linux中关于文件的IO(文件的创建、打开、写入、输出)

1.创建一个叫creat.txt的文件,

/*  
    int open(const char *pathname,int flags,mode_t mode);
        参数:
            -pathname:要打开文件的路径
            -flags:对文件的操作权限设置还有其他的设置
                必选项:O_RDONLY,O_WRONLY,O_RDWR 这三个设置是互斥的
                可选项:O_CREAT 文件不存在创建
            -mode:八进制的数,表示创建的新的文件的操作权限,比如:0775
             最终权限是:mode & ~umask
        返回值:返回一个新的文件描述符,如果调用失败,返回-1

        umask的作用:抹去部分的权限

    flags参数是一个int类型的数据,占4个字节,32位
    flags 32个位,每一位就是一个标志位
*/
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
int main(){

    //创建一个新文件,这中间的|的作用,就是需要所有的权限
    int fd =open("creat.txt",O_RDWR|O_CREAT,0777);

    if(fd==-1){
        perror("open");
    }

    close(fd);
    return 0;
}

2.打开一个叫a.txt的文件

/*  #include<sys/types.h>
    #include<sys/stat.h>
    #include<fcntl.h>

    int open(const char *pathname,int flags);
        参数:
            -pathname:要打开文件的路径
            -flags:对文件的操作权限设置还有其他的设置
             O_RDONLY,O_WRONLY,O_RDWR 这三个设置是互斥的
        返回值:返回一个新的文件描述符,如果调用失败,返回-1

    errno:属于Linux系统函数库,库里的一个全局变量,记录的是最近的错误号

    #include<stdio.h>
    void perror(const char *s);打印errno对应的错误描述
        s参数:用户描述,比如hello,最终输出的内容是 hello:***(实际错误描述)
    
    //创建一个新的文件
    int open(const char *pathname ,int flags,mode_t mode);

*/

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
int main(){
    int fd =open("a.txt",O_RDONLY);
    if(fd == -1){
        perror("open");
    }

    close(fd);
    return 0;
}

3.文件的写入与输出(将english.txt文件写入cpy.txt文件)

/*
    #include<unistd.h>
    ssize_t read(int fd,void *buf ,size_t count);
        参数:
            -fd:文件描述符,通过这个操作文件
            -buf:要读取数据存放的地方,就是数组的地址(传出参数)
            -count:指定数组的大小
        返回值:
            -成功:
                >0:返回实际读取到的字节数
                =0:已读完
            -失败:-1并且设置errno
        

    #include<unistd.h>
    ssize_t write(int fd,const void *buf,size_t count);
        参数:
            -fd:文件描述符,open得到的
            -buf:要往磁盘写入的数据
            -count:要写数据的实际大小
        返回值:
            -成功:
                >0:返回实际读取到的字节数
                =0:无读入
            -失败:-1并且设置errno
*/
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(){
    //通过open打开文件
    int srcfd=open("english.txt",O_RDONLY);
    if(srcfd==-1){
        perror("open");
    } 
    //创建新的文件
    int destfd=open("cpy.txt",O_WRONLY|O_CREAT,0664);
    if(destfd==-1){
        perror("open");
        return -1;
    } 
    //读写操作
    char buf[1024]={0};
    int len=0;
    while((len = read(srcfd,buf,sizeof(buf)))>0){
        write(destfd,buf,len);
    }

    close(srcfd);
    close(destfd);
    return 0;
}

4.文件的追加(在原有的基础上增加)

/*
    标准c库函数
    #include<stdio.h>
    int fseek(FILE *stream,long offset,int whence);

    Linux系统函数
    #include<sys/types.h>
    #include<unistd.h>

    off_t lseek(int fd,off_t offset,int whence);
        参数:
            -fd:文件描述符,open得到
            -offset:偏移量
            -whence:
                SEEK_SET:设置文件指针的偏移量
                SEEK_CUR:设置偏移量:当前位置+第二个参数offset的值
                SEEK——END:设置偏移量:文件大小+第二个参数offset的值
        返回值:返回文件指针的位置

    作用:
        1.移动到文件指针到文件头
        lseek(fd,0,SEEK_SET);
        2.获取当前文件指针的位置
        lseek(fd,0,SEEK_CUR);
        3.获取文件长度
        lseek(fd,0,SEEK_END);
        4.拓展文件的长度,当前文件10b,110b,增加了100个字节
        lseek(fd,100,SEEK_END);
*/

#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int main(){
    int fd=open("hello.text",O_RDWR);

    if(fd==-1){
        perror("open");
        return -1;
    }

    //拓展文件长度
    int ret=lseek(fd,100,SEEK_END);
    if(ret==-1){
        perror("lseek");
        return -1;
    }

    //写入点东西才能生效
    write(fd,"",1);

    //关闭文件
    close(fd);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值