【发布时间】:2012-11-25 07:56:12
【问题描述】:
可能重复:
What is the proper function for comparing two C-style strings?
我的匹配条件不起作用!有人可以建议如何与 C 风格的字符串进行比较吗?
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;
即使temp 和data 匹配,以下条件不起作用:
if (stpr==data) {
isData = true;
}
不确定这是否有帮助。从下面的函数调用SaveData() 函数:
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();
}
}
【问题讨论】:
-
对不起,是我的错。它正在比较 2 char*