Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting input string based on whitespace and commas

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:

a r3, r2, r1
ah r8, r5, r6, r7
ai r10, i10, r127

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.

My current implementation is the following:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <bitset>
using namespace std;

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


    if (inFile.is_open()){
        cout << "test" << endl;
        //Read until no more lines in text file to read
        //while (getline(inFile, instruction))
        while (inFile >> 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 << myString[i] << endl;
                i++;
            }

        }
    }
    system("pause");
}

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

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

a r3
r1
r2

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?

like image 973
Noobgineer Avatar asked Dec 14 '25 20:12

Noobgineer


1 Answers

This will do the job.

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

        istringstream ss(instruction);
        string token;
        //Separate string based on commas and white spaces
        getline(ss,token,' ');
        myString.push_back(token);
        cout << myString[i] << endl;
        i++;

        while (getline(ss, token,',')){
            ss.ignore();
            myString.push_back(token);
            cout << myString[i] << endl;
            i++;
        }

    }
}

Points:
getline(inFile,instruction) will get you entire line
getline(ss,token,' ') reads till white space
getline(ss,token,',') reads till comma
finally, ss.ignore(), to ignore a character in stream.

It is important to note that, getline stops if End of Stream is reached.


Your inFile>>instruction reads till while space by default. To read the entire line, you have to use getline instead.

like image 186
Rishit Sanmukhani Avatar answered Dec 17 '25 13:12

Rishit Sanmukhani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!