0

I want to process each line of a file, and then split it by a delimiter and get for example first and third token.

In python,

with open("filename.txt") as f:
    for line in f:
        tokens = line.split("|")
        print(tokens[0], tokens[2])

So, cpp, I can read the lines of a file with getline. Like this:

ifstream fin(f);
string myStr;

while( getline(fin, myStr)){

}

Now, how to split the myStr with | and get the tokens? I tried to call getline(myStr, token, '|') inside the while loop again, but it doesn't seem to work. Can someone please help?

Thanks

Ahsanul Haque
  • 9,145
  • 2
  • 27
  • 48
  • `getline(myStr, token, '|')` doesn't work because `getline` requires a `std::istream` as first parameter. You would need to use an intermediate `std::stringstream`. Not sure if it's the most efficient though. – churill Dec 01 '20 at 06:55
  • Also check out the discussion here on splitting on a delimiter in C++: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c – costaparas Dec 01 '20 at 06:57

1 Answers1

1

UPDATE: ONLY NEED THE FIRST THREE TO FOUR TOKENS

Try this in your while loop:

size_t pos = 0;
vector<std::string> tokens;
while ((pos = myStr.find("|")) != std::string::npos) {
    tokens.push_back(myStr.substr(0, pos));
    myStr.erase(0, pos + delimiter.length());
    if(tokens.size() == 4){
        break;
    }
}
tokens.push_back(myStr)

And initialise tokens outside and before the while loop:

vector<int> tokens;

MODIFIED VERSION OF AN ANSWER FROM THIS QUESTION: Parse (split) a string in C++ using string delimiter (standard C++)

An0n1m1ty
  • 398
  • 2
  • 13
  • Thanks. It works. However, I need another suggestion. `myStr` is too long, and contains like 70-80 tokens. However, the tokens I need are just the first 3-4. So, is there a way not to read the whole string, and split, and only read the first 3-4 and break? I tried, but it doesn't work. Should I post a separate question? – Ahsanul Haque Dec 01 '20 at 11:22
  • I've edited the code by adding `if(tokens.size() == 4){break;}`, which breaks the loop that looks for tokens when you have exactly four tokens i.e. the length of the vector `tokens` is four. Just adjust that number if you need more or less tokens. – An0n1m1ty Dec 02 '20 at 00:13