My input file is like this:
C:\Users\DeadCoder\AppData\Local\CoCreate
I am making a tree and I need to abstract the names of directories while reading from input file with \
delimiter. Like in the above example, i need to abstract separately c:, users, DeadCoder, Appdata .... I hope every one understands the questions.
Now Let us see the options that we got.
1-
istringstream
works perfectly fine for whitespace
but not for \
.
2-
strtok()
works on char. So I would have to change my string to char and I really don't want to do this.
3- Boost Tokenizer()
This one seems interesting and I don't have any familiarity with it in the past except I just googled it a while ago. I copied the code and it is like this:
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
using namespace boost;
int main(){
string tempStr;
ifstream fin;
fin.open("input.txt");
int i=0;
while (!fin.eof()){
getline(fin,tempStr);
char_separator<char> sep("\"); // error: missing terminating " character
tokenizer<char_separator<char>> tokens(tempStr, sep);
for (const auto& t : tokens) {
cout << t << "." << endl;
}
}
Now this gives the error that "error: boost/foreach.hpp: No such file or directory"
can Someone help me here. And Is there any other better way
to read the input file with \ delimiter
. Please don't use extensive codes like class tokenizer()
as I am still learning c++.
EDIT: I didn't have boost library installed therefore I was having this error. it would be much of favor if someone can explain a better way to tokenize
string without installing a third library.
Best; DeadCoder.
In C++ (and other language based on C) the \
character in a string or character literal is the escape character. It means it escapes the next character in the literal. This is so you can have, for example, a "
inside a string at all. To have a \
inside a string literal you need to escape the backslash by having two of them: "\\"
.
You can read more about the valid escape sequences in C++ e.g. in this reference.
As for the problem with Boost, you need to tell the compiler where you installed it. This is done in the project properties of your IDE.
If you want to tokenize without using a third-party library such as Boost, there are a couple of ways. One way could be to use std::istringstream
and std::getline
. Another fo use the find
and substr
functions of the standard string
class.