>#include读取代码段>#include >#include >#include >#include >#include >#include >#include >using namespace std;
使用ifstream 然后对C++进行文件的读取
int main() { string buffer, filepath = "test.txt"; ifstream myfile(filepath.c_str()); //等同于 ifstream myfile("test.txt"); while(getline(myfile, buffer)) { cout << buffer << endl; } myfile.close(); return 0;}
使用ofstream 然后对C++进行文件的写入
写入代码段
int main() { string filepath = "test.txt"; //ofstream myfile("test.txt"); ofstream myfile(filepath.c_str()); myfile << "hbhh" << endl; myfile << "aaaaa" << endl; myfile.close(); return 0;}