0

strtokの後に文字列を比較しようとしていますが、trueではなくfalseになっているようです

strtokを使用している場合、他に何かする必要がありますか?

char file[] = "temp.txt";
ifstream getfile;
getfile.open(file,ios::in);
if(getfile.is_open())
{
        char data[256];
    char *line;
    const char * test = "init";

    //loop till end of file                   
    while(!getfile.eof())
    {
        //get data and store to variable data
            getfile.getline(data,256,'\n');

        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," ");
        }

    }
}

出力:

 comparing init with init

必要な出力:

 comparing init with init
 init

ありがとう!:D

===========================

以下に変更して動作しました!:)

if(strcmp(line,test)==0)
4

2 に答える 2

3

コンテンツではなくポインタを比較しています。strcmpを調べるか、C文字列をstd::stringでラップします。

于 2012-05-15T21:25:59.593 に答える
2

ポインタ(文字列のアドレス)を比較しています。それらは常に異なります。strcmp()文字列自体を比較するために使用します。

于 2012-05-15T21:26:42.563 に答える