Linux中对文件的操作(一)

linux中对文件进行操作

open函数

*int open(const char pathname, int flags);
pathname:指的是文件名
flags:权限 只读打开O_RDONLY 只写打开O_WRONLY 可读可写打开O_RDWR 以上三个参数中应当只指定一个。

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


int main(){
//int open(const char *pathname, int flags);
        int fd;
        fd = open("./file1",O_RDWR);

        printf("fd = %d\n",fd);	//返回3,若file1不存在则返回-1
        return 0;
}

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

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

int main(){
        int fd;
        fd = open("./file1",O_RDWR);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);	//可读 r 4 可写 w 2 可执行 x 1;6=4+2 可读可写
                if(fd > 0){
                        printf("open file success!\n");
                }
        }
        return 0;
}

O_EXCL 如果同时指定了O_CREAT,而文件已经存在,则出错使fd=-1;

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

int main(){
        int fd;

        fd = open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);
        if(fd == -1){
                printf("file cunzai\n");
        }
        return 0;
}

运行结果:
file cunzai
因为当前目录中已经存在file1这个文件了。如果此时不存在,则运行这段代码不会报错,因此运行结果不会有任何反应。
O_APPEND 每次写都加到文件的尾端
在我们正常使用write写入数据时,会将自己的数据覆盖之前的数据(区别于O_TRUNC,O_APPEND不会清空之前的数据,而是覆盖之前的字节,如果之前的数据多于新写入的数据,那么多的部分不会被清空),如果我们想要在之前的基础上写入数据,就需要用到这个参数

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

int main(){
        int fd;
        char *buf = "wlw is so handsome";
        fd = open("./file1",O_RDWR|O_APPEND);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT|O_APPEND,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! fd = %d\n",fd);

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

        close(fd);


        return 0;
}

运行之后,会在原来的基础上换行写入数据。

O_TRUNC 打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0


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

int main(){
        int fd;
        char *buf = "test";
        fd = open("./file1",O_RDWR|O_TRUNC);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT|O_APPEND,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! fd = %d\n",fd);

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

        close(fd);


        return 0;
}

加入O_TRUNC参数会把原先的内容清空,再加入自己需要的内容。
此外open函数还可以添加第三个参数,mode_t mode,一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限。

write函数

*ssize_t write(int fd, const void buf, size_t count);
fd 通过open函数后,会返回一个文件描述符fd
buf 缓冲区
count 写入文件的大小
将缓冲区buf中的数据写count个字符进入到fd指向的文件中写入成功时,返回写入的字节数,读取失败返回-1.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.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 file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! fd = %d\n",fd);

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

        close(fd);


        return 0;
}

注意:在本段代码中,如果将write的第三个参数修改为sizeof(buf)只会显示8个字符,因为buf属于指针类型,sizeof(buf)只会分配指针的大小,在linux系统中为指针分配的8字节大小。

close

int close(int fd);
只需给出文件的fd即可关闭该文件

read

*ssize_t read(int fd, void buf, size_t count);
将fd指向文件的count大小的字符读入到buf中去。读取成功时,返回读取的字节数,读取失败返回-1.

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

int main(){
        int fd;
        char *buf = "wlw is handsome";
        fd = open("./file1",O_RDWR);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! 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 file\n",n_write);
        }

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read:%d byte,context:%s\n",n_read,readBuf);

        close(fd);


        return 0;
}

执行后会显示
open success! fd = 3
write 15 byte to file
read:1 byte,context:

内容被不能被读取出来,这就涉及了光标的问题,当我们写入字符后,光标会在最后一位上,那此时我们进行read操作,只会从最后开始读,自然什么也读不到。因此我们需要将光标移到头,或者重新打开。

重新打开解决光标问题


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

int main(){
        int fd;
        char *buf = "wlw is handsome";
        fd = open("./file1",O_RDWR);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! 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 file\n",n_write);
        }

        close(fd);      //关闭
        fd = open("./file1",O_RDWR);    //重新打开

        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read:%d byte,context:%s\n",n_read,readBuf);

        close(fd);


        return 0;
}

运行结果为:
open success! fd = 3
write 15 byte to file
read:15 byte,context:wlw is handsome
在write写完之后进行关闭重新打开,此时光标会回到第一个位置,就可以通过read进行读取,解决了光标的问题。

光标移动操作解决光标问题

lseek

off_t lseek(int fd, off_t offset, int whence);
将fd指向文件的文件读写指针(光标)相对whence移动offset个字节
whence:可以选择
SEEK_SET:头
SEEK_END :尾
SEEK_CUR:当前位置

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

int main(){
        int fd;
        char *buf = "wlw is handsome";
        fd = open("./file1",O_RDWR);

        if(fd == -1){
                printf("open file failed!\n");
                fd = open("./file1",O_RDWR|O_CREAT,0600);
                if(fd > 0){
                        printf("create file success!\n");
                }
        }
        printf("open success! 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 file\n",n_write);
        }

        //off_t lseek(int fd, off_t offset, int whence);        
        lseek(fd,0,SEEK_SET);	//移动光标
        char *readBuf;
        readBuf = (char *)malloc(sizeof(char)*n_write);
        //ssize_t read(int fd, void *buf, size_t count);
        int n_read = read(fd,readBuf,n_write);
        printf("read:%d byte,context:%s\n",n_read,readBuf);

        close(fd);


        return 0;
}

运行结果:
open success! fd = 3
write 15 byte to file
read:15 byte,context:wlw is handsome
另外在修改光标位置时,也可以利用SEEK_CUR或者SEEK_END来进行偏移,注意!偏移量为正数时向后偏移,偏移量为负数时向前偏移。

利用lseek函数来计算文件大小

lseek的返回值为当前光标较文件起始位置的偏移量,因此我们可以用lseek来计算出文件大小:

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

int main(){
        int fd;
        char *buf = "wlw is handsome";

        fd = open("./file1",O_RDWR);

        int filesize = lseek(fd,0,SEEK_END);
        printf("file size is :%d\n",filesize);

        close(fd);


        return 0;
}

运行结果:
file size is :15

creat

*int creat(const char pathname, mode_t mode);
根据创建模式(权限)创建文件
creat常见创建模式:
宏表示 数字
S_IRUSR 4 可读
S_IWUSR 2 可写
S_IXUSR 1 可执行
S_IRWXU 7 可读、写、执行

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

int main(){
        int fd;
        char *buf = "wlw is handsome";


        //int creat(const char *pathname, mode_t mode);
        fd = creat("./file1",S_IRWXU);
        printf("creat file1 success!\n");
        fd = open("./file1",O_RDWR);
        write(fd,buf,strlen(buf));
        printf("write file1 success!\n");
        return 0;
}

最后分享几个在linux命令行中的快捷操作
复制 yy
多行复制 行数+yy
粘贴 p
撤回 u
重做 Ctrl+r
删除整行 dd
删除大段代码 d+↓
缩进 左:<<
右:>>
多行缩进 行数+<<或者行数+>>

  • 15
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值