Comparing two char* for equality

c++, c-strings

Solution

Since both `stpr` and `data` are C strings, you need to use `strcmp()`:

#include <string.h>
...
if (strcmp(stpr, data) == 0) {
    // strings are equal
    ...
} else {
    // strings are NOT equal
}

Problem

Possible Duplicate: What is the proper function for comparing two C-style strings? My match condition doesn't work! Can someone advise how to compare to C-style strings? ``` void saveData(string line, char* data){ char *testString = new char[800]; char *stpr; int i=0; bool isData=false; char *com = data; strcpy(testString,line.c_str()); stpr = strtok(testString, ","); while (stpr != NULL) { string temp = stpr; cout << temp << " ===== " << data << endl; ``` Even though `temp` and `data` match, the following condition doesn't work: ``` if (stpr==data) { isData = true; } ``` Not sure if this helps. The `SaveData()` function is called from the function below: ``` void readFile(char* str){ string c="", line, fileName="result.txt", data(str); ifstream inFile; inFile.open(fileName.c_str()); resultlist.clear(); if(inFile.good()){ while(!inFile.eof()){ getline(inFile, line); if(line.find(data)!=string::npos){ cout << line << endl; } saveData(line, str); } inFile.close(); } } ```

Original source

Related problems