c++ - 使用 C++ 中的 for 循环逐个字符串读取文件行中的字符串
问题描述
我的 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
解决方案
推荐阅读
- javascript - Django:Ajax 未从服务器响应接收数据
- java - 在抽象类中指定长的“serialVersionUID”是否有意义?
- android - HttpURLConnection connect() 方法失败并崩溃
- python - SQLITE3 FROM 表选择列 WHERE 布尔语句
- javascript - 有什么方法可以在不使用 math.random() 函数的情况下在 javascript 中生成随机数?
- r - 使用 Donchian Channel 在 R 中的交易策略
- google-chrome - 阻止当前来源在“myRemoteSite”接收具有 MIME 类型 application/json 的跨站点文档
- reactjs - ReactJS - 数组中的 JSON 对象
- c# - 如何将arrayList值分配给C#中的相关标签
- javascript - 如何在 Java (JEE) 中读取 Ajax JSON?