In my C++ code, I want to read from a text file (*.txt) and tokenize every entry. More specifically, I want to be able to read individual words from a file, such as “format”, “stack”, “Jason”, “europe”, etc.
I chose to use fstream to perform this task, and I do not know how to set it’s delimiter to the ones I want to use (space, \n, as well as hyphens and even apostrophes as in “Mcdonal’s”). I figured space and \n are the default delimiters, but hyphens are not, but I want to treat them as delimiters so that when parsing the file, I will get words in “blah blah xxx animal–cat” as simply “blah”, “blah”, “xxx”, “animal”, “cat”.
That is, I want to be able to get two strings from “stack-overflow”, “you’re”, etc, and still be able to maintain \n and space as delimiters at the same time.
An istream treats “white space” as delimiters. It uses a locale to tell it what characters are white space. A locale, in turn, includes a ctype
facetthat classifies character types. Such a facet could look something like this:And a little test program to show it works:
Result:
istream_iterator<string>uses>>to read the individual strings from the stream, so if you use them directly, you should get the same results. The parts you need to include are creating the locale and usingimbueto make the stream use that locale.