文件的自动化操作(上)

这篇博客详细介绍了在Linux系统中如何使用open、creat函数进行文件的打开与创建,并通过write和read函数实现文件的读写操作。内容包括文件权限设置、光标移动以及结构体数组的写入。示例代码展示了如何在不同模式下打开文件,以及如何读取和写入数据到文件中。
摘要由CSDN通过智能技术生成

文件的打开及创建

SYNOPSIS

        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>
        int open(const char *pathname, int flags);
        int open(const char *pathname, int flags, mode_t mode);

pathname:要打开的文件名(含路径,缺省为当前路径)

Flags:

        O_RDONLY  只读打开

        O_WRONLY  只写打开

        O_RDWR  可读可写打开

当我们附带了权限后,打开的文件就只能按照这种权限来操作。

以上这三个常数中应当只指定一个。下列常数是可选择的:

       O_CREAT  若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。

        O_EXCL  如果同时指定了O_CREAT,而文件已经存在,则出错,一般用来判断文件是否已经存在。

        O_APPEND  每次写时都加到文件的尾端,防止写入时把前面已有的内容覆盖掉。

        O_TRUNC  属性取打开文件时,如果这个文件中本来是有内容的,而且为只读只写成功打开,则将其长度截断为0(把原有内容清空)。

文件权限

  1. 可读        r        4
  2. 可写        w       2
  3. 执行        x        1
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
        int fd;
        fd = open("./file1",O_RDWR);//打开当前程序路径“file1”文件,可读可写方式打开
        if(fd == -1){//文件打开成功返回>1的值,打开失败返回-1
                printf("Open file1 failed\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);//可读可写:4+2
                if(fd > 0){
                        printf("creat file1 success\n");
                }
        }
        return 0;
}

文件写入操作

SYNOPSIS
 

       #include <unistd.h>

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

fd:文件描述符   buf:无类型指针的缓冲区   count:写入文件的大小

        write功能可以理解为将缓冲区buf的指针指向内存里面的数据,写“count”这么多个字节到fp(刚刚打开的文件)里,写入成功返回一个写入个数的整型数,写入失败返回-1。

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
        int fd;
        char *buf = "Hello World!";
        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("fd = %d\n",fd);
//      ssize_t write(int fd, const void *buf, size_t count);
        write(fd,buf,strlen(buf));
        close(fd);
        return 0;
}

文件读取操作

SYNOPSIS

       #include <unistd.h>

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

        从fd指向的文件读取“count”个字节的数据放到“buf”里。

        文件读取时还需要注意光标移动的操作。

SYNOPSIS

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

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

        offset:偏移值        whence:固定点的位置

        SEEK_SET  指向文件的头

        SEEK_CUR  当前位置,偏移值整数往前移,复数往后移

        SEEK_END  指向文件的尾

        lseek调用成功返回文件大小(多少字节)。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(){
        int fd;
        char *buf = "Hello World!";
        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 sccess file1: fd = %d\n",fd);
                }
        }
//      ssize_t write(int fd, const void *buf, size_t count);
        int n_write = write(fd,buf,strlen(buf));
        if(n_write != -1){
                printf("write %d byte to the file1\n",n_write);
        }
        lseek(fd,0,SEEK_SET);//光标回到原点
        char *readbuf;
        readbuf = (char *)malloc(sizeof(char)*n_write + 2);
//      ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readbuf,n_write);
        printf("read %d context:%s\n",n_read,readbuf);
        close(fd);
        return 0;
}

创建文件create函数

int creat(const char *pathname, mode_t mode);

 pathname:要创建的文件名(包含路径、缺省为当前路径)

mode:创建模式        //可读可写可执行

常见创建模式:

宏表示数字
S_IRUSR4可读
S_IWUSR2可写
S_IXUSR1可执行
S_IRWXU7可读、写、执行
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(){
        int fd;
        char *buf = "Hello World!";
        fd = creat("/home/CLC/FILE/file2",S_IRWXU);
        return 0;
}

Linux系统默认的文件描述符:

012
标准(键盘)输入标准输出标准错误
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(){
        char readbuf[128];
        int n_read = read(0,readbuf,5);//键盘输入
        int n_write = write(1,readbuf,strlen(readbuf));//标准输出
        printf("\ndone!\n");
        return 0;
}

写结构体数组到文件

        缓存区不是只有char类型,可以写整数,结构体等等,写入整数或结构体的文件打开是一串乱码而并不会影响系统对文件的判断。

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

struct Test{

        int a;
        char b;
};
int main(){
        int fd;
        struct Test data[2] = {{100,'a'},{101,'b'}};
        struct Test data2[2];

        fd = open("./file3",O_RDWR);
        write(fd,&data,sizeof(struct Test)*2);
        lseek(fd,0,SEEK_SET);
        int n_read = read(fd,&data2,sizeof(struct Test)*2);
        printf("read %d\nread2 %c\n",data2[0].a,data2[0].b);
        printf("read %d\nread2 %c\n",data2[1].a,data2[1].b);
        close(fd);
        return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从入门到捕蛇者说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值