Linux中对文件的操作(一)

devtools/2024/9/20 2:08:58/

linux_0">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不存在则返回-1return 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+↓
缩进 左:<<
右:>>
多行缩进 行数+<<或者行数+>>


http://www.ppmy.cn/devtools/35097.html

相关文章

OpenNJet应用引擎——云原生时代的Web服务新选择

文章目录 OpenNJet应用引擎——云原生时代的Web服务新选择引言&#xff1a;数字化转型的推动力&#xff1a;OpenNJet应用引擎为什么选择OpenNJet&#xff1f; OpenNJet的核心优势1. 云原生功能增强2. 安全加固3. 代码重构与性能优化4. 动态加载机制5. 多样化的产品形态6. 易于集…

【携程笔试题汇总】[全网首发] 2024-05-06-携程春招笔试题-三语言题解(CPP/Python/Java)

&#x1f36d; 大家好这里是清隆学长 &#xff0c;一枚热爱算法的程序员 ✨ 本系列打算持续跟新携程近期的春秋招笔试题汇总&#xff5e; &#x1f4bb; ACM银牌&#x1f948;| 多次AK大厂笔试 &#xff5c; 编程一对一辅导 &#x1f44f; 感谢大家的订阅➕ 和 喜欢&#x1f49…

云原生周刊:Terraform 1.8 发布 | 2024.5.6

开源项目推荐 xlskubectl 用于控制 Kubernetes 集群的电子表格。xlskubectl 将 Google Spreadsheet 与 Kubernetes 集成。你可以通过用于跟踪费用的同一电子表格来管理集群。 git-sync git-sync 是一个简单的命令&#xff0c;它将 git 存储库拉入本地目录&#xff0c;等待一…

程序员做知识付费,做大纲时要注意那些点?

大纲的注意点 本节我们将讨论制作大纲时的要点。由于大家对大纲的制作已有一定了解&#xff0c;因此我们不会全面展开&#xff0c;而是聚焦于一些关键注意事项与大家分享。 条理性 最关键的一点是大纲必须具备清晰的条理性。如果大纲在逻辑结构上不够清晰&#xff0c;无论其他…

C#知识|将选中的账号信息展示到控制台(小示例)

哈喽&#xff0c;你好啊&#xff0c;我是雷工&#xff01; 上篇学习了控件事件的统一关联&#xff0c; 本篇通过实例练习继续学习事件统一处理中Tag数据获取、对象的封装及泛型集合List的综合运用。 01 实现功能 在上篇的基础上实现&#xff0c;点击选中喜欢的账号&#xff0…

定时器中断方式控制LED--第六天

1.思路&#xff1a; 1.1给定时器TMOD 初值 给初值定一个10ms TR0 1 开始计时 &#xff1b;TF0 0 计算爆表 ET0 1 打开定时器0中断 &#xff1b; EA 1 打开总中断EA 1.2.main()函数里面的软件定时消掉&#xff0c;用定时器定时 &#xff1b;定义一个中断函数&#xff0c;写入…

未授权访问

未授权访问是系统对用户限制不全&#xff0c;或者无限制&#xff0c;可以让任意用户或者限制访问用户&#xff0c;访问到需要权限认证的地址。未授权访问通常是会泄露用户信息&#xff0c;系统信息。某些服务和系统中&#xff0c;未授权访问还可以执行系统命令&#xff0c;操作…

拓扑是什么意思

拓扑学&#xff0c;有时候被称为“橡胶片几何学”&#xff0c;它是研究空间形状在连续变形下的性质的数学分支。这种变形包括拉伸、压缩、扭曲&#xff0c;但不包括撕裂或粘合。 一个经典的拓扑问题就是“环面和咖啡杯”的问题。在拓扑学中&#xff0c;一个环面&#xff08;就…