I need help with my C++ code, which reads data from an ASCII file and stores them into an std::vector<double>
. The data are stored consecutively (as in the example below). For some reasons I want to substitute the while(iss>>token)
with a for
loop, like I did for the first while
loop, when I use getline()
.
Here is the code and below the ASCII file example.
#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
}
}
}
The 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