Converting string to char* or using different file opening method

Nov 10, 2009 at 5:48pm
closed account (z8RoizwU)
Hi All,

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);

string line;
while(getline(FILE,line))
{
// char* cline = const_cast<line.c_str()>;
char* res = strtok (const_cast<line.c_str()>, " ");
if(strcmp((const char*)res, "DERIVED_OBJECT") == 0)


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.

Thanks for all the help
Nov 10, 2009 at 5:55pm
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).
Nov 10, 2009 at 5:57pm
Use stringstreams instead of strtok http://www.cplusplus.com/reference/iostream/stringstream/
and == instead of strcmp
Nov 10, 2009 at 6:07pm
strtok() modifies the string being parsed, which is bad.

If you really really want to use it, you should copy the line into a mutable buffer.
1
2
3
4
5
6
7
8
9
10
11
12
13
std::string line;
while (std::getline(is, line))
{
    if (char *dupline = new char[1 + line.size()])
    {
        strcpy(dupline, line.c_str());

        char *tok = strtok(dupline, " ");
        // ...

        delete [] dupline;
    }
}


You should ask yourself if you really want to use strtok. Personally, I avoid it and these days only maintain its use in existing code.
Last edited on Nov 10, 2009 at 6:08pm
Nov 10, 2009 at 6:28pm
closed account (z8RoizwU)
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...

Thanks
Nov 10, 2009 at 6:58pm
use strchr()
Nov 10, 2009 at 7:55pm
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()...
Nov 10, 2009 at 9:04pm
Sounds like a perfect job for std::string::first_first_of(), but since OP wants to use strtok() so much...
1
2
3
4
5
std::ifstream file("filename",std::ios::binary|std::ios::ate);
size_t l=file.tellg();
char *buffer=new char[l];
file.seekg(0);
file.read(buffer,l);

Some people only learn the hard way, you know?
Nov 11, 2009 at 5:49am
closed account (z8RoizwU)
hey thanks, the stdio.h library functions helped

also finally i need to compare a char* with a constant word

char* buff and the word "APPLE"

i am trying (buff == "APPLE"), but it doesnt work

what am i doing wrong . ?
Nov 11, 2009 at 5:54am
closed account (z8RoizwU)
presently i am using

if(strcmp((const char*)buff,"APPLE") == 0)

{
}

but is there a simpler way ?

thanks
Nov 11, 2009 at 9:26am
but is there a simpler way ?
Yes.
strcmp(buff,"APPLE") == 0

(Conversion from non-const to const can be implicit.)
Topic archived. No new replies allowed.