Saturday, May 22, 2010

C++ file open, read words and compare with a string?

tempWord = "Hello";


while (inFile %26gt;%26gt; testString) {


if (strcmp(tempWord, testString) == 0)


cout%26lt;%26lt;"work exists"%26lt;%26lt;endl;


}


testString and tempWord are strings so strcmp would work cause it needs character arrays i think.





i am not using objects because it is a simple program.

C++ file open, read words and compare with a string?
strcmp will fail because its a c library function looking for a c variable (in this case a c string). An easy fix to this is to take any string objects and call the .c_str() method. If both things are Strings however you should just use the == operator.


No comments:

Post a Comment