first line

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
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 */
}

Hi Peter87 !

thanks for your help it is working as i wanted.

Regards,

Ewa
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
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
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
Topic archived. No new replies allowed.