Linux之文件编程
一、理论知识
- 文件描述符
- 文件编程的一般步骤
- Linux文件管理简述
二、open打开文件函数
-
参数说明
Pathname:要打开的文件名(含路径,缺省为当前路径) Flags: O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开 当我们附带了权限后,打开的文件就只能按照这种权限来操作。 以上这三个常数中应当只指定一 个。下列常数是可选择的: O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。 O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。 O_APPEND 每次写时都加到文件的尾端。 O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。 Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
-
源码
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main(void) { int fd; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = open("./file1", O_RDWR|O_CREAT, 0600); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); } } return 0; }
三、write写入文件
- 参数说明
- 源码
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <unistd.h> #include <string.h> int main(void) { int fd; char *buf = "jiangyo handsome!"; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = open("./file1", O_RDWR|O_CREAT, 0600); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); write(fd, buf, strlen(buf)); close(fd); } } return 0; }
四、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(void) { int fd; int n_write; char *buf = "jiangyo handsome!"; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = open("./file1", O_RDWR|O_CREAT, 0600); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); n_write = write(fd, buf, strlen(buf)); close(fd); } } printf("write %d Byte\n",n_write); fd = open("./file1", O_RDWR); char *read_buf; read_buf = (char *)malloc(sizeof(char)*n_write + 1); int n_read = read(fd, read_buf, n_write); printf("read %d byte:%s\n", n_read, read_buf); free(read_buf); return 0; }
五、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(void) { int fd; int n_write; char *buf = "jiangyo handsome!"; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = open("./file1", O_RDWR|O_CREAT, 0600); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); n_write = write(fd, buf, strlen(buf)); } } printf("write %d Byte\n",n_write); //lseek(fd, 0, SEEK_SET); //lseek(fd, -n_write, SEEK_CUR); lseek(fd, -n_write, SEEK_END); char *read_buf; read_buf = (char *)malloc(sizeof(char)*n_write + 1); int n_read = read(fd, read_buf, n_write); printf("read %d byte:%s\n", n_read, read_buf); free(read_buf); return 0; }
- 读取文件字节数
#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(void) { int fd; int n_write; char *buf = "jiangyo handsome!"; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = open("./file1", O_RDWR|O_CREAT, 0600); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); n_write = write(fd, buf, strlen(buf)); } } printf("write %d Byte\n",n_write); int len = lseek(fd, 0, SEEK_END); printf("buf size = %d byte\n", len); close(fd); return 0; }
六、创建文件creat函数
- 参数说明
- S_IRWXU源码
#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(void) { int fd; int n_write; char *buf = "jiangyo handsome!"; fd = open("./file1", O_RDWR); if(fd == -1) { printf("creat fail\n"); fd = creat("./file1", S_IRWXU); fd = open("./file1", O_RDWR|O_TRUNC); printf("fd = %d\n", fd); if(fd > 0) { printf("creat file1 success\n"); n_write = write(fd, buf, strlen(buf)); } } else { fd = open("./file1", O_RDWR|O_TRUNC); printf("fd = %d\n", fd); if(fd > 0) { printf("open file1 success\n"); n_write = write(fd, buf, strlen(buf)); } } printf("write %d Byte\n",n_write); int len = lseek(fd, 0, SEEK_END); printf("buf size = %d byte\n", len); close(fd); return 0; }
七、实现文件拷贝
- 源程序
#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, fdDec; char *Readbuf=NULL; if(argc != 3) { printf("param error\n"); exit(-1); } fdSrc = open(argv[1], O_RDWR); int size = lseek(fdSrc, 0, SEEK_END); lseek(fdSrc, 0, SEEK_SET); Readbuf = (char *)malloc((sizeof(char)) * size + 8); read(fdSrc, Readbuf, size); fdDec = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0600); write(fdDec, Readbuf, size); close(fdSrc); close(fdDec); free(Readbuf); return 0; }
八、配置文件的修改
- 源程序
#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) { printf("param error\n"); exit(-1); } fdSrc = open(argv[1], O_RDWR); int size = lseek(fdSrc, 0, SEEK_END); lseek(fdSrc, 0, SEEK_SET); Readbuf = (char *)malloc((sizeof(char)) * size + 8); read(fdSrc, Readbuf, size); char *str = strstr(Readbuf, "LENG="); str = str+strlen("LENG="); *str = '5'; lseek(fdSrc, 0, SEEK_SET); write(fdSrc, Readbuf, size); close(fdSrc); free(Readbuf); return 0; }
九、整型数写入文件
- 源程序
#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(void) { int fd; int indata = 100; int outdata; fd = open("./file1", O_RDWR|O_CREAT, 0600); write(fd, &indata, sizeof(int)); lseek(fd, 0, SEEK_SET); read(fd, &outdata, sizeof(int)); printf("outdata = %d\n", outdata); close(fd); return 0; }
十、结构体写入文件
- 源程序
#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 t1[2] = {{100, 'a'},{200, 'b'}}; struct test t2[2]; fd = open("./file1", O_RDWR|O_CREAT|O_TRUNC, 0600); write(fd, t1, sizeof(struct test)*2); lseek(fd, 0, SEEK_SET); read(fd, t2, sizeof(struct test)*2); printf("t0.a = %d, t0.c = %c, t1.a = %d, t1.c = %c\n", t2[0].a, t2[0].c, t2[1].a, t2[1].c); close(fd); return 0; }