A
Alexander Stippler
I'm no expert to streams, so I need some help with the following program.
It takes a text file as input and reads the characters contained into a
std::vector<char>. But it does not work correctly. Giving it a file containing
just one letter, this letter gets inserted into the list twice. Why and how
to correct it? Here is the code:
#include <fstream>
#include <vector>
#include <stringstream>
#include <string>
template <typename T>
std::vector<T>
readParameterList(std::ifstream &in)
{
std::vector<T> ret;
std::string line;
T value;
getline(in, line);
std::istringstream lineStream(line);
while (!lineStream.eof()) {
lineStream >> value;
ret.push_back(value);
}
return ret;
}
int
main()
{
std::ifstream is("checkIt");
std::vector<char> v = readParameterList<char>(is);
std::cerr << v.size() << std::endl;
}
P.S.: the function shall work for other built-in types too, so it's a
template function.
regards,
Alex
It takes a text file as input and reads the characters contained into a
std::vector<char>. But it does not work correctly. Giving it a file containing
just one letter, this letter gets inserted into the list twice. Why and how
to correct it? Here is the code:
#include <fstream>
#include <vector>
#include <stringstream>
#include <string>
template <typename T>
std::vector<T>
readParameterList(std::ifstream &in)
{
std::vector<T> ret;
std::string line;
T value;
getline(in, line);
std::istringstream lineStream(line);
while (!lineStream.eof()) {
lineStream >> value;
ret.push_back(value);
}
return ret;
}
int
main()
{
std::ifstream is("checkIt");
std::vector<char> v = readParameterList<char>(is);
std::cerr << v.size() << std::endl;
}
P.S.: the function shall work for other built-in types too, so it's a
template function.
regards,
Alex