目录
1、fopen、fwrite、fseek、fread、fclose
一、Linux系统下一系列的API
1、API
打开 | 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记录待创建的文件的访问权限
#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 = "this is a test!";
fd = open("./file1",O_RDWR); //打开文件 可读写
if(fd == -1){ //返回值 失败-1
printf("open file1 failed\n");
fd = open("./file1",O_RDWR|O_CREAT,0600); //O_CREAT创建文件
if(fd > 0){
printf("create file1 success!\n");
}
}
printf("open susceess : 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);
// off_t lseek(int fd, off_t offset, int whence);
// lseek(fd, 0, SEEK_SET); //设置光标为文件头
// lseek(fd, -15, SEEK_CUR); //设置光标向前偏移15
lseek(fd, -n_write, SEEK_CUR);
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,100);
printf("read %d ,context:%s\n",n_read,readBuf);
close(fd);
return 0;
}
2、创建文件creat函数
#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 = "test";
// int creat(const char *pathname, mode_t mode);
fd = creat("/home/datou/file1",S_IRWXU);
return 0;
}
3、计算文件大小
#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 = "this is a test!";
fd = open("./file1",O_RDWR);
int filesize = lseek(fd, 0, SEEK_END);
printf("file's size is:%d\n",filesize);
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 fd;
char readBuf[128];
int n_read = read(0, readBuf,5);
int n_write = write(1,readBuf,strlen(readBuf));
printf("\ndone!\n");
return 0;
}
三、应用
1、实现Linux cp命令的代码
-
test
#include <stdio.h>
int main(int argc, char **argv)
{
printf("totol params: %d\n",argc);
printf("No.1 params :%s\n",argv[0]);
printf("No.2 params :%s\n",argv[1]);
printf("No.3 params :%s\n",argv[2]);
return 0;
}
-
实现cp命令
#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=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, size); //读取原文件到readBuf
fdDes = open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0600); //创建并打开新文件
int n_write = write(fdDes,readBuf,strlen(readBuf)); //写readBuf
close(fdSrc); //关闭两个文件
close(fdDes);
return 0;
}
2、修改程序的配置文件
#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); //读取原文件到readBuf
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;
}
四、写入一个整型数到文件
#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, &data2, sizeof(int));
printf("read %d \n",data2);
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 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;
}
六、总结open和fopen的区别
参考:总结open与fopen的区别 - NickyYe - 博客园
参考:fopen与open的区别(好文)_oscarjulia的博客-CSDN博客_fopen和open
七、标准C库对文件操作的引入
1、fopen、fwrite、fseek、fread、fclose
#include <stdio.h>
#include <string.h>
int main()
{
//FILE *fopen(const char *pathname, const char *mode);
FILE *fp;
char *str = "fopen test!";
char readBuf[128] = {0};
fp = fopen("./fopen.txt","w+");
//size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
int nwrite = fwrite(str,sizeof(char),strlen(str),fp);
//fwrite(str,sizeof(char)*strlen(str),1,fp);
fseek(fp,0,SEEK_SET);
//size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
int nread = fread(readBuf,sizeof(char),strlen(str),fp);
//fread(readBuf,sizeof(char)*strlen(str),1,fp);
printf("read data: %s\n",readBuf);
printf("write=%d,read=%d\n",nwrite,nread);
fclose(fp);
return 0;
}
2、写入结构体到文件
#include <stdio.h>
#include <string.h>
struct Test
{
int a;
char c;
};
int main()
{
FILE *fp;
struct Test data = {100,'a'};
struct Test data2;
fp = fopen("./CWrite.txt","w+");
int n_write = fwrite(&data,sizeof(struct Test),1,fp);
fseek(fp,0,SEEK_SET);
int n_read = fread(&data2, sizeof(struct Test),1,fp);
printf("read %d,%c\n",data2.a,data2.c);
fclose(fp);
return 0;
}
3、fgetc、fputc、feof
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
int i;
char *str = "this is a test!";
int len = strlen(str);
fp = fopen("./fputc.txt","w+");
for(i=0;i<len;i++){
fputc(*str,fp);
str++;
}
fclose(fp);
return 0;
}
feof :判断是否到文件尾
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
int i;
char c;
fp = fopen("./fputc.txt","r");
while(!feof(fp)){// nonezero if reach end of file
c = fgetc(fp);
printf("%c",c);
}
fclose(fp);
return 0;
}