#include <iostream> #include <fstream> #include <string> using namespace std; int main() { const char filename[]="f:\\text.txt"; ofstream o_file; ifstream i_file; string out_text; //写 o_file.open(filename); for(int i =0;i<=12;i++) { o_file<< "a" << i <<' ';//将内容写入文本 } o_file.close(); //读 i_file.open(filename); if(i_file.is_open()) { while(i_file>>out_text) cout << out_text << endl; } else cout<<"打开文件:"<<filename<<"时出错!"; i_file.close(); return 0; }