读写文件常用操作
本博客主要包含以下内容
- 读写文件
- eof()函数,解决最后一行读两遍(多读一行)的问题.
1. 头文件
#include <fstream>
2. 打开文件
//#写文件:
1. ofstream ofile("./outfile.txt",ios::app);
2. ofstream ofile2;
ofile2.open("./outfile2.txt",ios::app);
//#读文件:
1. ifstream infile("./outfile.txt");
2. ifstream infile2;
infile2.open("./outfile2.txt");
关于参数ios::app,可以参考这位博主的博客: ios::ate和ios::app在C++文件中的区别.
3. 写文件
float f = 1.7;
ofile << std::fixed << std::setprecision(3) << f << "," << endl;
关于参数:
fixed<