Take the 2-minute tour ×

Im tring to compare strings after strtok but i seem to be getting false instead of a true

is there anything else i need to do if im using strtok?

char file[] = "temp.txt";
ifstream getfile;
        char data[256];
    char *line;
    const char * test = "init";

    //loop till end of file                   
        //get data and store to variable data

        line = strtok(data," ");
        while(line != NULL)
            cout << "Comparing " << line << " with " << test <<endl;
            //This is suppose to print but it dosent
            if(line == test)
                cout << line << endl;

            line = strtok(NULL," ");


output :

 comparing init with init

wanted output:

 comparing init with init

thanks! :D


changed to the following and it worked! :)

share|improve this question
There is a lot to fix in that code. You should try and write it using C++. Seriously ask the question on how to tokenize a file in C++. –  Loki Astari May 15 '12 at 22:20
add comment

2 Answers

up vote 3 down vote accepted

You're comparing pointers not content. Look into strcmp or wrap the C-strings in a std::string.

share|improve this answer
add comment

You are comparing pointers (the addresses of the strings). They will always be different. Use strcmp() to compare the strings themselves.

share|improve this answer
add comment

Your Answer


