first line
May 3, 2012 at 4:19pm UTC
Hi Guys!
how to get the text from file before any space come
suppose i have file and it reads
1 2 3
aa this is a
bb this is b
cc this is c
i am only interested in aa, bb, cc not the rest of the text. or in other words ignore the text that is after space
BR
Ewa
Last edited on May 3, 2012 at 4:29pm UTC
May 3, 2012 at 5:03pm UTC
1 2 3 4 5 6 7
std::ifstream file("abc.txt" );
std::string str;
while (file >> str /* read next word */ )
{
std::cout << str << std::endl;
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n' ); /* ignore rest of line */
}
May 3, 2012 at 6:02pm UTC
Hi Peter87 !
thanks for your help it is working as i wanted.
Regards,
Ewa
May 3, 2012 at 6:14pm UTC
i have an idea for tht if u save ur file data to a string and then u can use substrigs to find ur desired results
May 3, 2012 at 7:01pm UTC
Hi Guys!
Is there any such a method in which I can use before or after this file.ignore(), to actually keep the rest of the line? I mean, the rest of the line shall be used.
Considering that the first column of the text file is an integer, something inside of the while loop like:
1 2 3 4
value = atoi(str.c_str());
(if value==1){
cout <<restOfTheLine<<endl;
}
Regards,
Ewa
Last edited on May 3, 2012 at 7:24pm UTC
May 4, 2012 at 10:12am UTC
Hi Guys !
Thanks to all of you for your help, i solved it
1 2 3 4 5 6 7 8 9 10 11
string str, description;
int value;
while (infile>>str){
//cout<<str<<endl;
getline(infile,description);
value=atoi(str.c_str());
if (value==1){
cout<<description<<endl;
}
}
Regards,
Ewa
Last edited on May 4, 2012 at 10:12am UTC
Topic archived. No new replies allowed.