I'm trying to figure out what is the best way of handling text input like if I were to use fscnaf in C.
The below seems to work for a text file that contains...
string 1 2 3
string2 3 5 6
As I want it too. It reads the individual elements on each line and puts them into their respective vectors. Would you say this is a good way of handling the input? The input will always start with a string and then followed by the same count of numbers on each line.
int main(int argc, char* argv[])
{
ifstream inputFile(argv[1]);
vector<string> testStrings;
vector<int> intTest;
vector<int> intTest2;
vector<int> intTest3;
string testme;
int test1;
int test2;
int test3;
if (inputFile.is_open())
{
while (!inputFile.eof())
{
inputFile >> testme;
inputFile >> test1;
inputFile >> test2;
inputFile >> test3;
testStrings.push_back(testme);
intTest.push_back(test1);
intTest2.push_back(test2);
intTest3.push_back(test3);
}
inputFile.close();
}
else
{
cout << "Failed to open file";
exit(EXIT_FAILURE);
}
return 0;
}
UPDATE
I have changed the while loop to this...is it any better?
while (getline(inputFile, line))
{
istringstream iss(line);
iss >> testme;
iss >> test1;
iss >> test2;
iss >> test3;
testStrings.push_back(testme);
intTest.push_back(test1);
intTest2.push_back(test2);
intTest3.push_back(test3);
}