First I do not want to use boost library. I have a program which is reading a line and saving it to a string:
ajf938 sshd Thu Mar 27 23:25 still logged in c110-20-242-157.mirnd4.nsw.optusnet.com.au
And then I am trying to split it into 5 string. However the size of each element in string will be changed. The other string can be like:
jcm995 sshd Thu Mar 27 15:18 - 15:19 (00:00) 192.131.251.2
What I already have is
pos=name.find(' ',0);
cout<<pos<<endl;
string username = name.substr(0,pos);
cout<<username<<endl;
And then I look for fist not character from that position:
find_first_not_of(' ');
But I think there should be other efficient way. Any idea will be appreciated.
UPDATE:
This is what I have done so far:
if(name.find('(',0))
{
string username;
string date;
string duration;
string hostname;
int val = 0;
istringstream iss(name, istringstream::in);
string word;
while( iss >> word )
{
if(val==0){
username = word;
}
else if(val>1 && val<6)
{
date +=" "+word;
}
else if(val==6){
duration = word;
}
else if(val == 7){
hostname=word;
}
val++;
}
cout<<"usename= "<<username << " date= "<<date <<" duration= "<<duration<<" hostname= "<<hostname<<endl;
}