以下为个人学习笔记整理。参考书籍《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()) |
| { |
| |
| |
| 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(); |