自分のブログ名

物理の空き地 by M.E_K

日々の学び、感じたことを書いております。ブログ移行中->https://physics-mek.com

移転しました。

リダイレクトします。

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に詰めた。自分の好きな型に変えるなどしてくれればよい。

注意

VsualStudio2019でコーディングする際、C++言語標準を
規定からISO C++17標準に変えてやる必要がある。