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"||");