Parsing
How can I put the lines of a file into an array, if I don't know the number of lines beforehand? Below is what I do in Python.
1 2
|
with open(file_input_samples, 'r') as f:
samples = [line.split()[1] for line in f][2:]
|
Use a vector of strings:
http://www.cplusplus.com/reference/vector/vector/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <fstream>
#include <string>
#include <vector>
int main(int argc, char* argv[]) {
std::fstream fin("input.txt");
std::vector<std::string> lines;
std::string line;
while(getline(fin, line)) {
lines.push_back(line);
}
return 0;
}
|
Topic archived. No new replies allowed.