C++ 文件读写
#include <iostream>
#include <fstream>
void fileoperation(string filename){
// 文件写入相关操作
ofstream outFile;
outFile.open("info.txt");
outFile << fixed;
outFile << "Make and model" << endl;
outFile << "Year" << endl;
outFile.close();
// 文件读取操作
ifstream inFile;
inFile.open(filename);
if (!inFile.is_open()){
cout << "Could not open the file" << filename << endl;
}
string value;
inFile >> value;
if (inFile.eof())
cout << "End of file reached;" << endl;
else if (inFile.fail())
cout << "Input terminated by data mismatch." << endl;
else
cout << "Input terminated for unknown reason." << endl;
inFile.close();
}
int main() {
fileoperation("info.txt");
return 0;
}