C++ fileを一行ごとに読む
移転しました。
リダイレクトします。
#include <filesystem> #include <string> namespace fs = std::filesystem; int main() { std::vector<double>vx; std::vector<double>vy; std::string filepath = "C:\\sample.txt" std::ifstream ifs (filepath); if (!ifs){ throw std::exception(("cannot open" + filepath).c_str()); } std::string str; while(getline(ifs,str)) { if (str.size() == 0) { continue; } if (str[0] == '#') { continue; } std::istringstream iss(str); std::string a,b; while(iss >>a >> b) { double d1 = std::stod(a); double d2 = std::stod(b); vx.push_back(d1); vx.push_back(d2); } } return 0; }
今回はdouble型に変え、vecotorに詰めた。自分の好きな型に変えるなどしてくれればよい。