1、创建,打开文件
1.1、API
1.int open(const char*pathname,int flags)
2.int open(const char*pathname,int flags,mode_t mode)
3.int creat(const char*pathname,mode_t mode)
返回值:文件描述符(确定是哪个文件)(返回一个非负数,-1表示没有此文件)
参数1:文件指针路径+文件名(./文件名)
参数2:打开方式(读O_RDONLY,写O_WRONLY,可读可写O_RDWR)(创建文件要和前面3个与,并且要写第三个参数O_CREAT)
参数3:说明该文件存取许可权限rw(可读可写0600)
1.2、补充
参数3
0:
6: r:4 w:2 x:1
0:同组
0:其他组
参数2:与|
O_CREAT:创建文件,要有参数3的许可权限
O_EXCL:与上面搭配用,如果文件存在则返回-1
O_APPEND:打开文件光标指向文件尾
O_TRUNC:清楚文件内容
3.creac函数
参数1:创建的文件名(/文件名,/绝对路径,./当前路径)
参数2:模式权限
S_IRUSR:可读 4
S_IWUSR:可写 2
S_IXUSR:可执行 1
S_IRWXU:可读可写可执行 7
2、读写文件
2.1、API
1.ssize_t write(int fd,const void* buf,size_t count)
2.ssize_t read(int fd,void* buf,size_t count)
返回值:读写的字节(失败返回-1)
参数1:文件描述符(操作哪个文件)
参数2:写什么进去,指针类型; 读到哪里,指针类型
参数3:写几个字节,用sizeof和strlen算
3、光标
3.1、API
1.off_t lseek(int fd,off_t offset,int whence)
返回值:针对文件头偏移多少字节(光标指向尾,可以返回文件大小)
参数1:文件描述符
参数2:偏移量,0不偏移,负数往前,正往后
参数3:光标移动到位置,SEEK_SET头,SEEK_END尾,SEEK_CUR当前位置
注:一定注意光标位置,读写都会使位置变化
4、操作文件原理
1.UNIX shell使用文件描述符0与进程标准输入结合,1与输出结合,2与错误输出结合
STDIN_FILEND:0
STDOUT_FILEND:1
STDERR_FILEND:2
例:int r=read(0,buf,5)
write(1,buf,strlen®)
2.静态文件和动态文件
静态:硬盘上存储的
动态:从硬盘上读取到内存,linux内核结构体
操作的是内存,也就是动态文件
操作完成后close,动态文件就更新到静态文件里,最终改变静态文件
3.为什么不直接操作静态文件
静态文件是以块存放的,操作不灵活
内存是按字节操作的,可以随机操作很灵活
5、c库函数
FILE*fp
1.fopen,fwrite,fread,fseek,fclose
fwrite参数:写什么指针类型,写多少字节,写多少次,fp
2.写读返回值:写返回写多少次,读返回读了几个字节
fopen返回地址,打开失败返回NULL
fgetc,fputc,feof
写,读,判断是否到文件尾
注:写读一个一个写读用for
feof返回值:没到尾返回0,到尾返回非0
6、应用实践
6.1、cp实现
- 判断参数是否正确
- 打开文件,lseek计算文件大小,malloc分配buf,read
- 创建或打开目标文件,wirte
- 关闭2文件,free内存空间
/***********************************************************************************
cp命令实现
***********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv)
{
int fd; //cp到的文件描述符
int cpfd; //cp的文件描述符
int rsize = 0, wsize = 0; //读写的个数
int size = 0; //文件大小
char *buf = NULL; //读缓冲区
char input[10] = {'\0'}; //用户输入缓存
if(argc < 3){ //判断main参数个数是否正确
printf("Parameter error\n"); //参数错误
goto Parameter; //错误处理
}
printf("The number of parameters is correct...\n"); //参数数量正确
cpfd = open(argv[1], O_RDONLY); //打开cp的文件
if(cpfd < 0){
perror("target File open failed"); //打开失败,输出失败原因
goto Parameter;
}
printf("%s open success...\n", argv[1]); //文件打开成功
size = lseek(cpfd, 0, SEEK_END); //计算文件大小
if(size < 0){
perror("lseek error"); //失败并输出失败原因
goto LseekError;
}
printf("document size %d...\n", size); //文件大小
buf = (char *)malloc(sizeof(char)*size); //分配内存空间
if(buf == NULL){
printf("malloc error\n"); //失败并输出失败原因
goto LseekError;
}
printf("malloc success...\n"); //内存分配成功
if(lseek(cpfd, 0, SEEK_SET) < 0){ //光标指向头
perror("lseek error"); //失败并输出失败原因
goto LseekError;
}
printf("Cursor movement success...\n"); //光标移动成功
rsize = read(cpfd, buf, size); //读文件
if(rsize < 0){
perror("read error"); //失败并输出失败原因
goto MallocError;
}
printf("read %d byte success...\n",rsize); //读取个数成功
fd = open(argv[2], O_WRONLY|O_CREAT|O_EXCL, 0600); //打开或创建文件
if(fd < 0){ //有错误
if(errno == 17){
printf("File exists\n"); //错误是因为文件存在
printf("Do you want to continue opening,yes/no?\n"); //是否继续打开
scanf("%s",input); //输入yes打开文件并清除内容
if(!strcmp("yes", input)){
printf("Reopening file...\n"); //正在重新打开
fd = open(argv[2], O_WRONLY|O_TRUNC);
}else{
perror("target File open failed");
goto MallocError;
}
}
}
if(fd < 0){ //有错误
perror("target File open failed");
goto MallocError;
}
printf("%s open success...\n", argv[2]); //文件打开成功
wsize = write(fd, buf, size); //写文件
if(wsize < 0){
perror("write error"); //失败并输出失败原因
goto WriteError;
}
printf("wead %d byte success...\n",wsize); //写入个数成功
close(fd);
free(buf);
close(cpfd);
printf("close %s success...\n",argv[2]);
printf("malloc Memory release success...\n");
printf("close %s success...\n",argv[1]);
return 0;
WriteError:
close(fd);
printf("close %s success...\n",argv[2]);
MallocError:
free(buf);
printf("malloc Memory release success...\n");
LseekError:
close(cpfd);
printf("close %s success...\n",argv[1]);
Parameter:
return -1;
}
6.2、修改配置文件
- 判断参数是否正确
- 打开文件,lseek计算文件大小,malloc分配buf,read
- 查找子串位置strstr,修改配置
- 移动光标,写入文件write
- 关闭文件,free内存空间
/***********************************************************************************
修改配置文件
参数2个,pz,SPEED
***********************************************************************************/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
int main(int argc, char **argv)
{
int fd; //文件描述符
int rsize = 0, wsize = 0; //读写的个数
int size = 0; //文件大小
char *buf = NULL; //读缓冲区
char *p = NULL;
if(argc < 3){ //判断main参数个数是否正确
printf("Parameter error\n"); //参数错误
goto Parameter; //错误处理
}
printf("The number of parameters is correct...\n"); //参数数量正确
fd = open(argv[1], O_RDWR); //打开文件
if(fd < 0){
perror("target File open failed"); //打开失败,输出失败原因
goto Parameter;
}
printf("%s open success...\n", argv[1]); //文件打开成功
size = lseek(fd, 0, SEEK_END); //计算文件大小
if(size < 0){
perror("lseek error"); //失败并输出失败原因
goto LseekError;
}
printf("document size %d...\n", size); //文件大小
buf = (char *)malloc(sizeof(char)*size); //分配内存空间
if(buf == NULL){
printf("malloc error\n"); //失败并输出失败原因
goto LseekError;
}
printf("malloc success...\n"); //内存分配成功
if(lseek(fd, 0, SEEK_SET) < 0){ //光标指向头
perror("lseek error"); //失败并输出失败原因
goto LseekError;
}
printf("Cursor movement success...\n"); //光标移动成功
rsize = read(fd, buf, size); //读文件
if(rsize < 0){
perror("read error"); //失败并输出失败原因
goto MallocError;
}
printf("read %d byte success...\n",rsize); //读取个数成功
p = strstr(buf, argv[2]); //找子串
if(p == NULL){
printf("No substring found\n");
goto MallocError;
}
p = p+strlen(argv[2])+1; //修改配置文件
*p = '9';
if(lseek(fd, 0, SEEK_SET) < 0){ //光标指向头
perror("lseek error"); //失败并输出失败原因
goto MallocError;
}
wsize = write(fd, buf, size); //写文件
if(wsize < 0){
perror("write error"); //失败并输出失败原因
goto MallocError;
}
printf("wead %d byte success...\n",wsize); //写入个数成功
free(buf);
close(fd);
printf("malloc Memory release success...\n");
printf("close %s success...\n",argv[1]);
return 0;
MallocError:
free(buf);
printf("malloc Memory release success...\n");
LseekError:
close(fd);
printf("close %s success...\n",argv[1]);
Parameter:
return -1;
}
/*
SPEED=5
LENG=100
SCORE=90
LEVEL=95
*/