-
infile String
hey guys,
I have a question to ask
I am going to take in a file with a list of ip addresses
ex 130.5.3.6
128.10.5.0
i need a way to take in the entire line then break up the string into the 4 strings for each octet.
That would mea the string of "130.5.3.6"
will be broken up into
string1 = "130"
string2 = "5"
string 3 = "3"
string 4 "6"
can you guys figure out a way to do that
-
My preferred method of splitting string is the Boost.StringTokenizer library.
http://www.boost.org/
-
Boost is cool. If you feel that it is overkill, you could use a stringstream to parse the string, and/or use getline with '.' as the delimiter.
-
Im using a getline with a '.' as the delimiter but I don't know how to get the rest of the ip address.
I can get the first octant but not the other 3
-
-
Code:
while(! in.eof() )
{
string line = "";
getline(in,line);
int i = 0;
while(line[i] != '.')
{
oct1 = oct1 + line[i];
i++;
}
cout<<oct1<<" ";
i++;
while(line[i] != '.')
{
oct2 = oct2 + line[i];
i++;
}
cout<<oct2<<" ";
i++;
while(line[i] != '.')
{
oct3 = oct3 + line[i];
i++;
}
cout<<oct3<<" ";
i++;
while(i != line.length())
{
oct4 = oct4 + line[i];
i++;
}
cout<<oct4<<endl;
}
This is the code I have so far. It takes in each octed but it only does it for one line. Why is that? shouldn't it be doing it for every line(every IP Address)?
-
I formatted the code better, Sorry.
-
I don't see why it would stop at one line, it might depend on the input file you are using.
I did notice that you are using eof() to control your outer while loop, which won't work in this case because eof() will return true only after your attempt to read using getline fails and after your code attempts to use the data that wasn't read in. Change your while to:
Code:
string line;
while(getline(in,line))
...