在C ++中使用for循环在文件行中逐个字符串读取

问题描述

我需要有关C ++代码的帮助,该C ++代码从ASCII文件读取数据并将其存储到std::vector<double>中。数据被连续存储(如下例所示)。由于某些原因,我想用while(iss>>token)循环替换for,就像我在使用while时对第一个getline()循环所做的那样。

这是代码,在ASCII文件示例下面。

#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <stdio.h>     
#include <stdlib.h>

using namespace std;

int main(int argc,char** argv){

 std::ifstream file("example_file_ASCII");
 std::vector<double> tmp_table;
 std::string line;
 
 //while(std::getline(file,line)){
 for( string line; getline( file,line ); ){
   std::istringstream iss(line);
   std::string token;

   while ((iss >> token)){
     if (some conditions)
       tmp_table.push_back(std::stod(token));
     else (other conditions)
       //jump some lines for example
     }
  }
  

}

example_file_ASCII

1221  91.  94.  96.  98.  100.  102.  104.  106.  108.  110.  114.  118.  121.
 125.  127.  128.  131.  132.  134.  137.  140.  143.  147.  151.  155.  159.
 162.  166.  170.  173.  177.  180.  182.  186.  191.  194.  198.  202.  205.
 //the file continues

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)