convert string to char*

I have part of my code that i do not know how to solve the error. my input in the file is something like.
age, (4, years, 5, months)
age, (8, years, 7, months)
age, (4, years, 5, months)


1
2
3
4
5
6
7
8
9
10
11
char * point
ifstream file;
file.open(file.c_str());
if(file.is_open())
{
while(file.good())
{
(getline(file, take));
point =strtok(take,", ()");
}
}

im trying to remove all the delimiters and that is bothering me very much. How can i convert take into char * to solve the error which reads cannot convert"std::string to char*"? i have tried changing my take into take.c_str() but its still the same.

Last edited on
&take[0]
Thanks alot! That solved my error.
strtok works with character arrays. You may not pass an object of type std::string to the function. Also strtok changes the original character array. So you may not use neither take.c_str() nor take.data().
I would suggest to use std::istringstream( take ) instead of strtok.
Thanks alot for your suggestion. i will start reading on how to implement that. I'm currently still trying to split the data input. when i tried running the code the 1st line was ok. But subsequent line has age missing.

1
2
3
4
5
6
7
8
9
age
4
years
5
months
8
years
7
months


do you know the reason why?
Last edited on
Show your updated code.
1
2
3
4
5
6
7
8
9
10
11
char * point;
ifstream file;
file.open(file.c_str());
if(file.is_open())
{
while(file.good())
{
(getline(file, take));
point =strtok(&take[0], ", ()");
}
}



also i tried using istringstream but the program crashed when i entered my file name.


1
2
3
4
5
6
7
8
9
10
11
12
char * point;
char take[256];
ifstream file;
file.open(file.c_str());
if(file.is_open())
{
while(file.good())
{
cin.getline(take, 256);
point =strtok(take,", ()");
}
}
Topic archived. No new replies allowed.