views:

38

answers:

1

I have a file in which each line contains two numbers. The problem is that the two number are separated by a space, but the space can be any number of blank spaces. either one, two, or more. I want to read the line and store each of the numbers in a variable, but I'm not sure how to tokenize it.

i.e
1 5
3 2
5    6
3  4
83         54
23 23
32   88
8         203
+4  A: 

Read each line, stick the contents of the line into a stringstream, and then read the two int out of the line:

std::string line;
while (std::getline(myfilestream, line))
{
    std::stringstream ss(line);
    int i, j;
    if (ss >> i >> j)
    {
        // use i and j
    }
}

If you know for a fact that each line will have exactly two ints (i.e., you absolutely, positively trust your source), you can read the values directly from the stream.

James McNellis
thanks. worked perfectly.