下面的代碼起到這樣一個作用:
如果有flow.txt文件,則打開該文件,在文件中繼續寫入字符串;
如果沒有該文件,則創建該文件並寫入。
#include<string> #include<fstream> #include <iostream>
std::ofstream file; if (file.bad()) { std::cout << "cannot open file" << std::endl;;
} file.open("flow.txt", std::ios::app); file << flow << "\n";