0

I know I can't use getline when I want to delimit on more than one character, so what do I use in its place? For this code example, I want to use two pipe symbols || instead of one pipe symbol |. I need to use vector, not strstr or strtok. Is there another function besides getline that will let me delimit on multiple charcters? Here is my code for one pipe symbol:

wchar_t *sGroups = L"group1||group2||group3";
wstringstream wSS(sGroups);
wstring wOut;
vector<wstring> vGroup;
while (wSS.good())
    {
    //getline(wSS, wOut, L'||');  // <- this does not work with getline
    getline(wSS, wOut, L'|');
    vGroup.push_back(wOut);
    }

size_t i = 0;
wchar_t sGroup[256]{};
while (i < vGroup.size())
{
if (vGroup[i].length() > 0)
    {
    StringCchCopyW(sGroup, 256, vGroup[i].c_str());

    // do something with sGroup
    }
i++;
}

Answer: For the replacement of getline, I modified the function referenced at multiple character for wchar_t:

std::wstring getlinemultiple(std::wistream& in, std::wstring delimiter)
{
std::wstring cr;
wchar_t delim = *(delimiter.rbegin());
size_t sz = delimiter.size(), tot;

do 
    {
    wstring temp;
    getline(in, temp, delim);
    cr += temp + delim;
    tot = cr.size();
    }
while ((tot < sz) || (cr.substr(tot - sz, sz) != delimiter));

return cr.substr(0, tot - sz);  // or return cr; if you want to keep the delimiter
}

And the getline statement changes to:

wOut = getlinemultiple(wSS, L"||");

JeffR
  • 765
  • 2
  • 8
  • 23
  • 1
    OT: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) Your `while (wSS.good())` loop is just the same. – Some programmer dude Jul 26 '23 at 11:36
  • As for your problem, one possible solution is to read a whole line, one by one, and then tokenize it yourself. For example you can use the [`find`](https://en.cppreference.com/w/cpp/string/basic_string/find) function to find each `"||"` sequence, and use the position returned to create a [sub-string](https://en.cppreference.com/w/cpp/string/basic_string/substr). Loop until there's nothing found, and create a sub-string of the last part. Using pen and paper to design code like this is usually a good idea. – Some programmer dude Jul 26 '23 at 11:37
  • You could also look at it like a special form of CSV file, and can search for a library which can handle multi-character separators to help you. – Some programmer dude Jul 26 '23 at 11:39
  • 1
    You don't use anything "in its place". You write your own code to parse input and look for multiple delimiters. There's nothing magical about `std::getline`, it's just a dozen or so lines of C++ code that reads an istream one character at a time, until a specific char gets read, and stuffs everything into a string. You'll just do a small variation of that. – Sam Varshavchik Jul 26 '23 at 11:41
  • Get familiar with boost spirit. Nice library which cam make this task fast and simple. – Marek R Jul 27 '23 at 12:12

0 Answers0