文章目录
一、什么是文件编程?
- 在windows中,我们如何编写一个文件呢?
步骤:打开/新建文档---->编辑文档---->保存文档---->关闭文档。 - 那在Linux中如何编写一个文件呢?
步骤:打开(open)---->读写(read/write)---->关闭(close) - 文件编程目的
关心如何用代码操作文件。实现文件创建,打开,编辑等自动化执行
二、常用文件函数
- 文件打开/创建:open()函数
- 创建文件:creat()函数
- 文件写入:write()函数
- 文件读取:read()函数
- 关闭文件:close()函数
学会以上函数的使用
三、例子
-
打开写入读取
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <string.h> #include <stdlib.h> int main(){ int fd; char writebuf[128]; char *readbuf; int filesize = 0; int n_write, n_read; // 记得要用if判断 打开、读取、写入是否成功 if((fd = open("./file1", O_RDWR)) == -1){ printf(" open file1 failed!\n"); // fd = open("./file1", O_RDWR|O_CREAT|O_TRUNC, 0600); fd = open("./file1", O_RDWR|O_CREAT, 0600); if(fd > 0){ printf("create file1 success!\n"); }else{ printf("create file1 failed!\n"); exit(-1); } }else{ printf("open file1 success!\n"); } puts("input:"); gets(writebuf); if((n_write = write(fd, writebuf, strlen(writebuf))) == -1){ perror("write"); exit(-1); } filesize = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); readbuf = (char *)malloc(sizeof(char)*filesize + 1); if((n_read = read(fd, readbuf, n_write)) == -1){ perror("read"); exit(-1); } printf("read %d from file readbuf:%s\n", n_read, readbuf); close(fd); return 0; }
if
判断之后适用perror
和exit
函数找出错误并退出程序 -
自定义一个cp函数 cp file1 file2
实现方法:1.打开Src; 2.复制src内容到buf缓冲区; 3.打开Des; 4.从buf复制到Des; 5.关闭文件#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 argc, char **argv){ int fdSrc, fdDes; char *readbuf = NULL; // 参数数量判断 if(argc != 3){ puts("param error!"); exit(-1); } // argv[1] 为字符串数据, open第一个参数 目录缺省为当前文件夹 if((fdSrc = open(argv[1], O_RDWR)) == -1){ perror("fdSrc"); exit(-1); } // 求算字符串长度 int filesize = lseek(fdSrc, 0, SEEK_END); lseek(fdSrc, 0, SEEK_SET); readbuf = (char *)malloc(sizeof(char) * filesize + 1); int n_read = read(fdSrc, readbuf, filesize); close(fdSrc); if((fdDes = open(argv[2], O_RDWR|O_CREAT, 0600)) == -1){ perror("fdDes"); exit(-1); } lseek(fdDes, 0, SEEK_SET); // 相当于O_TRUNC,写之前移动光标到开头 int n_write = write(fdDes, readbuf, strlen(readbuf)); close(fdDes); // puts("Operate Success!!!"); return 0; }
-
修改存档中的数据,现有如下文档数据
SPEED=3 LENG =3 // 修改LENG = 66 SCORE=9 LEVEL=5
实现方法:从file1读取内容到buf缓冲区,用strstr函数查找字符串LENG,修改内容,写入file1
代码:#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 argc, char **argv){ int fdSrc; char *readbuf = NULL; if(argc != 2){ puts("param error!"); exit(-1); } // 打开file1 if((fdSrc = open(argv[1], O_RDWR)) == -1){ perror("fdSrc"); exit(-1); } // 读取数据到buf int filesize = lseek(fdSrc, 0, SEEK_END); lseek(fdSrc, 0, SEEK_SET); readbuf = (char *)malloc(sizeof(char) * filesize + 1); int n_read = read(fdSrc, readbuf, filesize); // 查找LENG内容,用指针p定位到要求改内容的位置,并判断是否存在该内容 char *p = strstr(readbuf, "LENG ="); if(p == NULL){ printf("NO exist the string!!\n"); } p = p+strlen("LENG ="); // 修改为自定义内容 puts("inputs datas!"); gets(p); // 写回到file1 lseek(fdSrc, 0, SEEK_SET); // O_TRUNC int n_write = write(fdSrc, readbuf, strlen(readbuf)); return 0; }
关于
strstr(haystack, needle)
函数:在字符串 haystack 中查找第一次出现字符串 needle 的位置,返回值为指针 -
写
整数、结构体、结构体数组
到文件#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> struct Test { int a; char c; }; int main() { int fd; struct Test data = {100,'a'}; struct Test data2; fd = open("./file1",O_RDWR); // 传入的参数为结构体的指针,其他类型参数等同 int n_write = write(fd,&data,sizeof(struct Test)); lseek(fd,0,SEEK_SET); int n_read = read(fd, &data2, sizeof(struct Test)); printf("read %d,%c \n",data2.a,data2.c); close(fd); return 0; }
四、标准C语言库的文件操作(open与fopen的区别)
面试常问: 总结open与fopen的区别
注意
fgetc
使用的时候会存在内部指针,每次使用都会向后偏移一个字节
如何使用:标准C库打开创建文件读写文件以及光标移动?
五、面试常问sizeof与strlen的区别
sizeof
是操作符,而strlen
是库函数。sizeof
的参数可以是数据的类型,也可以是变量,而strlen
参数只能是结尾为’\0’的字符串sizeof
在编译器编译时就计算出结果,而strlen
必须在运行时才能计算出来。sizeof
计算数据类型占内存的大小(计算指针的长度),strlen
计算字符串实际长度。sizeof
的参数为数组不退化, 传递给strlen
时数组会被退化为指针;
使用小技巧:遇见要分配内存的用sizeof,遇见要输出字符串长度的用strlen。
-
举例:
分配内存:
readBuf = (char *)malloc(sizeof(char) * len + 1); // 预留给字符串结尾'\0'
计算字符串长度:
int n_write = write(fdDes, readBuf, strlen(readBuf)); filesize = lseek(fd, 0, SEEK_END);
tips:
gets
puts
strlen
strstr
等函数参数均为指针或者字符串
参考链接:
Linux系统编程——文件编程:https://blog.csdn.net/qq_52902991/article/details/131261337
Linux系统编程之文件编程(一)
https://blog.csdn.net/weixin_46899589/article/details/130716525