I am using ifstream to open a file and then storing the contents line by line into a string using getline()
Now when I have to tokenize the string using strtok(), I am facing issues, becuase the argument there is char* and what I have is a string. What should I do ---
Using string.c_str() doesnt help, because that returns a const char*, and that wouldnt work
Is there some file opening method which rather saves the contents of the file in a char* buffer, which can then be used directly in my program. The relevant code is :
ifstream FILE;
FILE.open ("labellog.emd", ifstream::in);
Tried using the ugly const_cast, but it still doesnt work .
Also I have to then compare this res with a certain word (DERIVED_OBJECT) so if anything else can be suggested , I would be very happy.
First, if you give us a better idea of what you are trying to do perhaps someone can make a recommendation that does not involve the strtok function.
Second, you could create a std::vector<char> and copy each string into it so that you have a modifiable copy. I'm not sure that you want to read the file as binary because you'd have to find the carriage returns yourself (if this is a text file). but you could do that. If you read the file as binary you will have to do a bit more to parse the data and find the end of each line. you could use getline and read each line into a character buffer but then there is risk of a buffer overflow.
I would try posting some details on the requirements because I think that option 1 is best (avoid strtok altogether and find a more C++ like method for accomplishing the task).
The requirement is to get a particular part of the string in between some characters :
for eg :
string is : dsfewrf ewref:234dsf:dsafr:dsfgret
then i want the "dsafr" part from it. I have strtoked it with : as the delimiter and iteratively getting the tokens till a particular counter value is reached, pointing to the required substring
On the contrary, is there absolutely any way to open the file and reading it into a char* buffer rather than a sting that i am doing now using getline. that would solve my problem very easily...
If you're dead set on using legacy C functions, then use fopen() and fgets(), which are both meant to work with char* along with strtok(). That's not really the right way to do it, but since you are resistant to moving away from strtok()...