I ran into the problem of reading msg from a file using C ++. Usually people create a file stream and then use the function getline()to extract the msg. The function getline()can take an additional parameter as a separator, so that it returns each "string" separated by a new separator, but not the default "\ n". However, this delimiter must be char. In my usecase, it is possible that the delimiter in msg is something else like "| - |", so I'm trying to get a solution so that it accepts the string as a delimiter instead of char.
I searched StackOverFlow a bit and found interesting posts.
Analysis (division) row in C ++ using lines of restrictor (standard C ++)
It gives a solution to use string::find(), and string::substr()to parse an arbitrary delimiter. However, all the solutions there involve inputting a string, not a stream. In my case, the file stream data is too large / waste to fit directly into memory, so it should read in msg via msg (or most of the messages at once).
Actually, read the gcc implementation of the function std::getline(), it seems that it is much easier to handle the case delimiter - this is singe char. Since every time you load a piece of characters, you can always find a separator and separate them. Although the difference between the separator is more than one char, the separator itself can move between two different pieces and cause many other angular cases.
Not sure if anyone else came across this requirement before and how you guys adjusted it elegantly. It would seem nice to have a standard function, for example istream& getNext (istream&& is, string& str, string delim)? It seems to be a common sense to me. Why not do it in the standard library so that people can no longer implement their own version separately?
Many thanks