Reputation: 53
I am writing a small program that will read a plain ASCII text file containing 3 lines in 5 records, as follows:
f_name l_name
ID#
int int int int
I successfully put the first 2 lines in the 1D arrays where they belong, but I am having trouble putting the series of ints in the 2D array. My closest approach to the solution has been using the line:
studentScores[row][col] = atoi(input.c_str());
However, atoi parses only the first number, then throws away the rest of the line. I need to put each number in the string in a separate element of the array. I tried using stringstream, but I cannot get it to work correctly; apparently, the function I want to use is included in a different version of stringstream than I am using.
What could I use to parse this string?
Upvotes: 1
Views: 3437
Reputation: 62975
#include <string>
#include <sstream>
// ...
int ints[4];
std::string input;
std::getline(stream, input);
std::istringstream(input) >> ints[0] >> ints[1] >> ints[2] >> ints[3];
(Error handling omitted for brevity.)
Upvotes: 2