目录
一、理论
文件编程的内容超过,如:文件系统原理及访问机制,文件在内核中的管理机制,什么是文件信息节点inode,文件的共享,文件权限,各种用户对其权限......
我们在开始学习的时候应该以应用为王,比如:账单,游戏进度,配置文件......
关系如何使用代码操作文件,实现文件创建,打开,编辑等自动化执行
window如何手动修改文件,比如写一个word文档:执行过程应该是这样的,打开/创建文档 -> 编辑文档 -> 保存文档 -> 关闭文档
Linux呢?
计算机如何帮助我们自动化完成以上操作?操作系统提供了一系列的api
如linux系统:打开:open,读写:write/read,光标定位:lseek,关闭:close
打开/创建文件
参数说明
Pathname:要打开的文件名(含路径,缺省为当前路径)
Flags:
O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开
当我们附带了权限后,打开的文件就只能按照这种权限来操作。
以上这三个常数中应当只指定一 个。下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。
O_APPEND 每次写时都加到文件的尾端。
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限
创建文件creat函数
写入文件
读取文件
文件“光标”位置
将文件读写指针相对whence移动offset个字节
关闭文件
文件描述符
文件编程的一般步骤:打开/创建文件 -> 读取文件/写入文件 -> 关闭文件
linux文件管理简述
二、编程实战
一、文件 file
1. demo1:文件的打开
2. demo2:无文件,在创建
3. demo3:文件的写入
4. demo4:文件的读取。
demo5:光标问题通过,关/开文件解决
5. demo6:重新定位光标,用lseek()函数。
demo7:用lseek()函数计算文件里面字节的大小
6. demo8:O_EXCL()函数的用法。
demo9:O_APPEND()函数的用法。
demo10:O_TRUNC()函数的用法。
demo11:creator()函数用法 ls file2 -l -rwx------可读可写可执行
7. demo12:Linux系统默认:0标准输入;1标准输出;2标准错误
8. demo13:实现linux cp命令的代码,test1.c实现参数,gcc demo13.c -o mycp,生成mycp,./mycp demo13.c aaa.c,拷贝成功aaa.c
./a.out des src
totol params: 3
No1 params:./a.out
No2 params:des
No3 params:src
9. 解决demo13中两个小bug,把read函数中的1024改为size,加O_TRUNC()函数
10. demo14 配置文件的修改,配置文件为TEST.config ./a.out TEST.config
11. demo15 如果写进去是整型数,则配置文件为TEST.config,会出现乱码。
demo16 写入整数,输出结果也是乱码,但是不影响程序对它读数。
demo17 写入结构体。
12. demo18 写入结构体数组,输出结果也是乱码,但是不影响程序对它读数
13. 总结open和fopen的区别:https://www.cnblogs.com/NickyYe/p/5497659.html
一句话总结一下,就是open无缓冲,fopen有缓冲。前者与read, write等配合使用, 后者与fread,fwrite等配合使用。
14. 对fopen fread fwrite fseek fclose / fgetc fputc feof 的讲解。
demo19 用到了fopen fwrite fread。
demo20 一次全部读写完,进行1次。
15. demo21 fread fwrite的返回值,取决于第三个参数,返回int类型的次数。
demo22 把返回次数改为1,写是前提,读是后者
16. demo23 ---cp demo17.c demo23.c,都用f函数了。
demo24 使用fopen打开,fputc往里写。
demo25 fopen打开 fgetc获取
feof来判断是否到达文件尾巴,如果没有到达文件尾巴,这个文件的返回值为0,
demo1.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd;
fd = open("./file1",O_RDWR);
printf("fd = %d\n",fd);
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo1.c
CLC@Embed_Learn:~/file$ ./a.out
fd = 3
demo2.c
#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 file1 failed fd = %d\n",fd);
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success! fd = %d\n",fd);
}
}
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo2.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 failed fd = -1
create file1 success! fd = 4
demo3.c
#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[] = "123456789abcd";
//chari* buf = "123456789abcd";
fd = open("./file1",O_RDWR);
if(fd = -1){
printf("open file1 failed fd = %d\n",fd);
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success! fd = %d\n",fd);
}
}
printf("open file1 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;
}
CLC@Embed_Learn:~/file$ gcc demo3.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 failed fd = -1
create file1 success! fd = 4
open file1 success :fd = 4
demo4.c
#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 = "123456789abcd";
fd = open("./file1",O_RDWR);
if(fd = -1){
printf("open file1 failed fd = %d\n",fd);
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success! fd = %d\n",fd);
}
}
printf("open file1 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 file1\n",n_write);
}
char *readBuf;
readBuf = (char *)malloc(sizeof(char)*n_write + 1);
// 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;
}
CLC@Embed_Learn:~/file$ gcc demo4.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 failed fd = -1
create file1 success! fd = 4
open file1 success :fd = 4
write 13 byte to file1
read 0,context:
demo5.c
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd;
char *buf = "123456789abcd";
fd = open("./file1",O_RDWR);
if(fd = -1){
printf("open file1 failed fd = %d\n",fd);
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success! fd = %d\n",fd);
}
}
printf("open file1 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 + 1);
// 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;
}
CLC@Embed_Learn:~/file$ gcc demo5.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 failed fd = -1
create file1 success! fd = 4
open file1 success :fd = 4
write 13 byte to file
read 13,context:123456789abcd
demo6.c
#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 = "123456789abcd";
fd = open("./file1",O_RDWR);
if(fd = -1){
printf("open file1 failed fd = %d\n",fd);
fd = open("./file1",O_RDWR|O_CREAT,0600);
if(fd > 0){
printf("create file1 success! fd = %d\n",fd);
}
}
printf("open file1 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 + 1);
// ff_t lseek(int fd, off_t offset, int whence);
lseek(fd,0,SEEK_SET);//针对文件头偏移值是0
// lseek(fd,-(n_write),SEEK_CUR);//针对文件当前值,移值-n_write
// 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;
}
CLC@Embed_Learn:~/file$ gcc demo6.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 failed fd = -1
create file1 success! fd = 4
open file1 success :fd = 4
write 13 byte to file
read 13,context:123456789abcd
demo7.c
#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 = "123456789abcd";
fd = open("./file1",O_RDWR);
int filesize = lseek(fd,0,SEEK_END);
printf("file's size is: %d\n",filesize);
close(fd);
return 0;
}
file1
123456789abcd
CLC@Embed_Learn:~/file$ ./a.out
file's size is: 13
demo8.c
#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("file1 have\n");
}
return 0;
}
CLC@Embed_Learn:~/file$ ./a.out
file1 have
demo9.c
#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[] = "123456789abcd";
//chari* buf = "123456789abcd";
fd = open("./file1",O_RDWR|O_APPEND);
printf("open file1 success :fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte to file\n",n_write);
}
close(fd);
return 0;
}
file1
123456789abcd
CLC@Embed_Learn:~/file$ gcc demo9.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 success :fd = 3
write 13 byte to file
file1
123456789abcd123456789abcd
demo10.c
#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[] = "123456789abcd";
fd = open("./file1",O_RDWR|O_TRUNC);
printf("open file1 success :fd = %d\n",fd);
int n_write = write(fd,buf,strlen(buf));
if(n_write != -1){
printf("write %d byte tu file\n",n_write);
}
close(fd);
return 0;
}
file1
123456789abcd123456789abcd
CLC@Embed_Learn:~/file$ gcc demo10.c
CLC@Embed_Learn:~/file$ ./a.out
open file1 success :fd = 3
write 13 byte to file
file1
123456789abcd
demo11.c
#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[] = "123456789abcd";
// int creat(const char *pathname, mode_t mode);
fd = creat("./file2",S_IRWXU);
close(fd);
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo11.c
CLC@Embed_Learn:~/file$ ./a.outCLC@Embed_Learn:~/file$ ls file2 -l
-rwx------ 1 CLC book 0 Aug 27 13:55 file2
demo12.c
#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 readBuf[128];
int n_read = read(0,readBuf,5);
int n_write = write(1,readBuf,strlen(readBuf));
printf("\ndone\n");
close(fd);
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo12.c
CLC@Embed_Learn:~/file$ ./a.out
hello
hello
done
demo13.c
#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;
int fdDes;
// char readBuf[1024] = {0};
char *readBuf = NULL;
if(argc != 3){
printf("pararm 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);//动态申请内存
// int n_read = read(fdSrc,readBuf,1024);
int n_read = read(fdSrc,readBuf,size);
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600);
int n_write = write(fdDes,readBuf,strlen(readBuf));
close(fdSrc);
close(fdDes);
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo13.c -o mycp
CLC@Embed_Learn:~/file$ ./mycp demo13.c aaa.cCLC@Embed_Learn:~/file$ cat aaa.c
demo14.c
#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("pararm 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);
int n_read = read(fdSrc,readBuf,size);
char *p = strstr(readBuf,"LENG=");
if(p==NULL){
printf("not found\n");
exit(-1);
}
p = p+strlen("LENG=");
*p = '5';
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
TEST.config
PEED=5
LENG=3
SCORE=90
LEVEL=95
CLC@Embed_Learn:~/file$ cat TEST.config
PEED=5
LENG=3
SCORE=90
LEVEL=95CLC@Embed_Learn:~/file$ ./a.out TEST.config
CLC@Embed_Learn:~/file$ cat TEST.config
PEED=5
LENG=5
SCORE=90
LEVEL=95
demo15.c
#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("pararm 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);
int n_read = read(fdSrc,readBuf,size);
char *p = strstr(readBuf,"LENG=");
if(p==NULL){
printf("not found\n");
exit(-1);
}
p = p+strlen("LENG=");
*p = 5;
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
return 0;
}
CLC@Embed_Learn:~/file$ gcc demo15.c
CLC@Embed_Learn:~/file$ ./a.out TEST.config
CLC@Embed_Learn:~/file$ vi TEST.config
PEED=6
LENG=^E
SCORE=90
LEVEL=95看起来LENG=是乱码,实际上对于计算机是正确的,对于人眼来说是错的
demo16.c
#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;
int data = 100;
//int data2 = 0;
fd = open("./file1",O_RDWR);
int n_write = write(fd,&data,sizeof(int));
lseek(fd,0,SEEK_SET);
int n_read = read(fd,&data,sizeof(int));
printf("read %d\n",data);
//int n_read = read(fd,&data2,sizeof(int));
//printf("read %d\n",data2);
close(fd);
return 0;
}
CLC@Embed_Learn:~/file$ touch file1
CLC@Embed_Learn:~/file$ gcc demo16.c
CLC@Embed_Learn:~/file$ ./a.out
read 100
CLC@Embed_Learn:~/file$ vi file1d^@^@^@
demo16 写入整数,输出结果也是乱码,但是不影响程序对它读数。
demo17.c
#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);
int n_read = read(fd,&data,sizeof(struct Test));
printf("read %d,%c\n",data.a,data.c);
close(fd);
return 0;
}
CLC@Embed_Learn:~/file$ touch file1
CLC@Embed_Learn:~/file$ gcc demo17.c
CLC@Embed_Learn:~/file$ ./a.out
read 100,aCLC@Embed_Learn:~/file$ vi file1
d^@^@^@a^@^@^@
demo17 写入结构体数组,输出结果也是乱码,但是不影响程序对它读数
demo18.c
#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[2] ={{100,'a'},{101,'b'}};
struct Test data2[2];
fd = open("./file1",O_RDWR);
int n_write = 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,%c\n",data2[0].a,data2[0].c);
printf("read %d,%c\n",data2[1].a,data2[1].c);
close(fd);
return 0;
}
CLC@Embed_Learn:~/file$ touch file1
CLC@Embed_Learn:~/file$ gcc demo18.c
CLC@Embed_Learn:~/file$ ./a.out
read 100,a
read 101,b
CLC@Embed_Learn:~/file$ vi file1
d^@^@^@a^@^@^@e^@^@^@b^@^@^@
demo18 写入结构体数组,输出结果也是乱码,但是不影响程序对它读数
demo19.c