I need to read a text file into a set of tokens. To do this, I would like to read the file into a string and then tokenize the string. How do I read a text file into one large string? Is it possible to tokenize the text file directly?
I need to read a text file into a set of tokens. To do this, I would like to read the file into a string and then tokenize the string. How do I read a text file into one large string? Is it possible to tokenize the text file directly?
One way is to use getline() to read it to a string, then concatenate the string to the result string.How do I read a text file into one large string?
Yes, or at least I think it is a yes.Is it possible to tokenize the text file directly?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Code:std::ostringstream ostr; ostr << in_file.rdbuf(); std::string contents = ostr.str();
Are you getting an error?
Maybe you don't have this:
Code:#include <sstream>
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I forgot the #include <sstream>. I know basic C++, but string and character manipulation is a bit of a weak spot.
I've managed to copy the contents of a text file into a string. Now I would like to break up this string into a set of smaller strings. I was planning to use the strtok function, but it appears that this function will only operate on character arrays. Any suggestions are to how I should tokenize my text file (or my string)?
std::vector<char> should work just as well as a char array.
You could use a stringstream to tokenize your file contents, or find and substr. There are also more powerful tools available. Which is best depends on how you want to separate the text.