-
formatted file input
ok, ive been working on this for about 3 hours. ive stared at the STL references for about an hour. i know the problem is that i need a good algorithm, so looking in the STL references wont help. anyways, heres teh problem.
i have a file that is something like this:
10,5,2,3
now, i want the parts seperated by the commas in seperate variables(int). the limit is 4 variables(must be compatible with any number u to 4). i have written a function written that takes two std::string and a char then takes everything from the beginning to the comma and stores that in the first string. the other part is put into the second string. the comma is deleted.
i think that my way of thinking isnt very good for these kinda problems, so any help, or a finger to the right direction will be appreciated.
-
This compiles and works in g++.
Code:
#include <string>
#include <vector>
#include <sstream>
int main() {
std::istringstream in("1,1,2,3,5,8,13,21,34"); // replace in with std::ifstream if neccesary
std::vector<int> intVec;
while (!in.eof()) {
int temp;
in >> temp;
intVec.push_back(temp);
char eatComma;
in >> eatComma;
}
std::cout << "index\tvalue" << std::endl;
for (size_t i = 0; i < intVec.size(); i++) {
std::cout << i << "\t" << intVec[i] << std::endl;
}
return 0;
}