ファイルオープンのモードについて
実行ファイルと同じ階層にある“dat.txt”を読込モードでオープンする例
#include <iostream> #include <fstream> int main() { std::ofstream ofs("dat.txt", std::ios::out); if (ofs.fail()) { std::cout << "ファイルが開けませんでした。" << std::endl; return 0; } ofs << "あいうえお かきくけこ" << std::endl; std::cout << "データを書き込みました。" << std::endl; ofs.close(); retrun 0; }
実行ファイルと同じ階層にある“dat.txt”を書き込みモードでオープンする例
#include <iostream> #include <fstream> int main() { std::ifstream ifs("dat.txt", std::ios::in); if (ifs.fail()) { std::cout << "ファイルが開けませんでした。" << std::endl; return 0; } //この辺でごにょごにょ読み込みを行う //ifs.close(); retrun 0; }
[09/09 9:13] 佐藤 陽悦
https://cpprefjp.github.io/reference/sstream.html
[09/09 9:13] 佐藤 陽悦
sstream:文字列をストリームとして使う→文字列分割
[09/09 9:14] 佐藤 陽悦
https://www.delftstack.com/ja/howto/cpp/how-to-read-a-file-line-by-line-cpp/
行単位読み込み
[09/09 9:14] 佐藤 陽悦
同じく