以下为个人学习笔记整理。参考书籍《C++ Primer Plus》
# 文件输入输出
# 文件输入
#include<fstream> | |
// 写文件 | |
std::ofstream write_file;  | |
// 文件不存在会创建新的。 | |
write_file.open("iof_file.txt");  | |
write_file << "hello world" << endl << "save the world";  | |
write_file.close();  | 

# 文件输出
#include<fstream> | |
// 读文件 | |
string read_val; | |
std::ifstream read_file;  | |
read_file.open("iof_file.txt");  | |
if (!read_file.is_open())return;  | |
while (read_file.good())  | |
{ | |
    // 会忽略空格 | |
    /*read_file >> read_val; | |
cout << read_val;*/  | |
    // 这样就不会 | |
getline(read_file, read_val);  | |
cout << read_val << endl;  | |
} | |
if (read_file.eof()) {  | |
cout << "read over" << endl;  | |
} | |
else if (read_file.fail()) {  | |
cout << "read fail!!!" << endl;  | |
} | |
read_file.close();  | 
