Splitting input string based on whitespace and commas
<p>I am reading in formatted lines from a file and need to parse it and output some results to a file. The trouble i am having is properly parsing the input to deal with whitespaces and commas. The format of lines in the file can be of the following:</p>

<pre><code>a r3, r2, r1
ah r8, r5, r6, r7
ai r10, i10, r127
</code></pre>

<p>So i can have have 4-5 different elements in the line. The first element is a "command", then a whitespace, then the values that are separated with commas and whitespaces.</p>

<p>My current implementation is the following:</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;
#include &lt;bitset&gt;
using namespace std;

void main()
{
    string instruction;
    vector&lt;string&gt; myString;
    ifstream inFile;
    inFile.open("myfile.txt");
    int i = 0;


    if (inFile.is_open()){
        cout &lt;&lt; "test" &lt;&lt; endl;
        //Read until no more lines in text file to read
        //while (getline(inFile, instruction))
        while (inFile &gt;&gt; instruction)
        {

            istringstream ss(instruction);
            string token;
            //Separate string based on commas and white spaces
            while (getline(ss, token,',')){
                //getline(token, token, ' ');

                //Push each token to the vector
                myString.push_back(token);
                cout &lt;&lt; myString[i] &lt;&lt; endl;
                i++;
            }

        }
    }
    system("pause");
}
</code></pre>

<p>But this does not work. If i test it with <code>a r3, r2, r1</code>, i only get <code>a</code> and the program stops reading since it sees the white space.</p>

<p>The other version i tried is to change the reading of the file to <code>while (getline(inFile, instruction))</code>, but then this splits my string into:</p>

<pre><code>a r3
r1
r2
</code></pre>

<p>It thinks a(space)r3 is one element. Its splitting the entire line correctly, based on the commas, but the initial read is incorrect since i need it to also split a and r3. How can i do this?</p>