Reputation: 199
I need to read some lines from an input file into vector (of integers) and c++ is new to me so I'm having trouble understanding huge codes with lots of functions. Can you tell me how to do this the most basic way?
In my file I have something like this:
5 6 11 3 4
2 3 1
1
9
I wrote the input file with another program so I can present in it the number of vectors (4 in this case) and their size (5, 3, 1, 1) if I makes the reading easier.
I means I can present the information in any form... just need to know which one is better and how to use it.
Upvotes: 1
Views: 13102
Reputation: 480
Here is a simple example where std::istringstream
is used to extract the values of each line.
(Note that it is not necessary to call eof() before reading.)
std::ifstream file("file.txt");
std::vector<std::vector<int>> vectors;
std::string line;
while (std::getline(file, line))
{
std::istringstream ss(line);
std::vector<int> new_vec;
int v;
while (ss >> v) // populate the new vector
{
new_vec.push_back(v);
}
vectors.push_back(new_vec); // append it to the list of vectors
}
file.close();
Upvotes: 1
Reputation: 96013
It's not really 'a basic way', but it's short and it works:
#include <fstream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::vector<std::vector<int>> vec;
std::ifstream file_in("my_file.txt");
if (!file_in) {/*error*/}
std::string line;
while (std::getline(file_in, line))
{
std::istringstream ss(line);
vec.emplace_back(std::istream_iterator<int>(ss), std::istream_iterator<int>());
}
}
Slightly simplified version that does the same thing:
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
int main()
{
std::vector<std::vector<int>> vec;
std::ifstream file_in("my_file.txt");
if (!file_in) {/*error*/}
std::string line;
while (std::getline(file_in, line)) // Read next line to `line`, stop if no more lines.
{
// Construct so called 'string stream' from `line`, see while loop below for usage.
std::istringstream ss(line);
vec.push_back({}); // Add one more empty vector (of vectors) to `vec`.
int x;
while (ss >> x) // Read next int from `ss` to `x`, stop if no more ints.
vec.back().push_back(x); // Add it to the last sub-vector of `vec`.
}
}
I wouldn't call stringstreams a basic feature, but doing what you want without them will be a lot more messy.
Upvotes: 4