1、文件
- 程序文件:源程序文件、目标文件、可执行文件
- 数据文件:文本文件、二进制文件
2、C语言文件接口
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
int fclose(FILE *stream);
//读二进制文件
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
//写二进制文件
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
//读字符
int fgetc(FILE *stream);
//写字符
int fputc(int c, FILE *stream);
//读文本行
char *fgets(char *s, int size, FILE *stream);
//写文本行
int fputs(const char *s, FILE *stream);
- r:只读
- w:只写
- a:追加
- b:二进制文件操作 (*不独立出现,与前几个组合使用)
- +:读写 (*不独立出现,与前几个组合使用)
(1)二进制文件读写:fread/fwrite
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
char buf[1024] = {};
const char * msg = "write successful\n";
void write_file()
{
FILE * fp = fopen("myfile", "w");
if(!fp)
{
cout<<"Error"<<endl;
return;
}
fwrite(msg, strlen(msg), 1, fp);
fclose(fp);
}
void read_file()
{
FILE * fp = fopen("myfile", "r");
if(!fp)
{
cout<<"Error"<<endl;
return;
}
while(feof(fp) == 0)
{
if(fread(buf, 1, strlen(msg), fp) > 0)
{
cout<<buf;
}
}
fclose(fp);
}
int main()
{
write_file();//写文件
read_file();//读文件
fwrite(msg, strlen(msg), 1, stdout);//消息写到显示器
return 0;
}
(2)文件读写:fgets/fputs
#include<stdio.h>
#include<iostream>
#include<cstring>
using namespace std;
char buf[1024] = {};
const char * msg = "myfile2\n";
void write_file()
{
FILE * fp = fopen("myfile2", "w");
if(fp)
{
fputs(msg, fp);
fclose(fp);
}
}
void read_file()
{
FILE * fp = fopen("myfile2", "r");
if(fp)
{
fgets(buf, 5, fp);
fclose(fp);
}
cout<<buf<<endl;
}
int main()
{
write_file();
read_file();
return 0;
}
(3)文件的随机读写
#include <stdio.h>
//根据文件指针位置和偏移量定位文件指针
int fseek(FILE *stream, long offset, int whence);
//返回文件指针相对于起始位置的偏移量
long ftell(FILE *stream);
//让文件指针位置回到文件起始位置
void rewind(FILE *stream);
(4)文件结束判定
注:不能用feof
函数返回值直接判断文件是否结束,应该在文件读取结束时,判断是读取失败结束还是遇到文件尾结束。即判断是feof
引起的结束还是ferror
引起的结束
- 文本文件:
((c=fgetc(fp))==EOF)
时结束(c类型为int,作为putchar(int)的参数) - 二进制文件:
(fread(ptr,size,nmemb,fp)<=0)
时结束(size为所读每个元素字节大小,nmemb为需要读几个)
下例为完整的文本文件读写和二进制文件读写:
文本文件读写:
#include<stdio.h>
#include<iostream>
using namespace std;
const char * msg = "This is file.txt!\n";
void write_file()
{
FILE * fp = fopen("file.txt", "w");
if(!fp)
{
cout<<"Error"<<endl;
return ;
}
fputs(msg, fp);
fclose(fp);
}
void read_file()
{
FILE * fp = fopen("file.txt", "r");
if(!fp)
{
cout<<"Error"<<endl;
return ;
}
int c;//注意此处为int,作为putchar(int)的参数
while((c = fgetc(fp)) != EOF)
{
putchar(c);
}
if(ferror(fp))
{
cout<<"I/O Error End!"<<endl;
}
fclose(fp);
}
int main()
{
write_file();
read_file();
return 0;
}
二进制文件读写:
#include<stdio.h>
#include<iostream>
#define SIZE 5
using namespace std;
const double arr[SIZE] = {0.1,0.2,0.3,0.4,0.5};
double res = 0.0;
void bin_write_file()
{
FILE * fp = fopen("bin_file.bin", "wb");
if(!fp)
{
cout<<"Error"<<endl;
return ;
}
fwrite(arr, sizeof(double), SIZE, fp);
fclose(fp);
}
void bin_read_file()
{
FILE * fp = fopen("bin_file.bin", "rb");
if(!fp)
{
cout<<"Error"<<endl;
return ;
}
while(fread(&res, sizeof(double), 1, fp)>0)
{
cout<<res<<endl;
}
if(ferror(fp))
{
cout<<"I/O Error End!"<<endl;
}
fclose(fp);
}
int main()
{
bin_write_file();
bin_read_file();
return 0;
}
来源:CSDN
作者:_NoBug_
链接:https://blog.csdn.net/qq_41245381/article/details/103924862