【发布时间】:2013-09-19 19:50:51
【问题描述】:
当我尝试在 C++ 中比较两个字符串时(一个从文件中读取,另一个分配了一个值),我得到一个不正确的输出:
ifstream pgmFile;
pgmFile.open(fileName);
string temp;
string comp = "P5";
for(int i = 0; i < 2; i++)
{
pgmFile >> noskipws >> temp;
cout << temp;
}
if(temp == comp)
{}
else
cout << "File does not contain 'P5'. Please provide the correct type of file." << endl;
理论上这应该返回“true”这两个字符串是正确的。 temp = "P5" 的输出,所以我不明白为什么每次都会遇到我的 else 情况。非常感谢任何帮助!
【问题讨论】:
-
确认
temp确实包含“P5”而不是“P5”。看看这是否能提供任何见解:cout << '|' << temp << '|' << temp.size(); -
你的温度是多少?看起来你阅读了 nonskipws istream 两次,而你的 temp 是你第二次阅读的内容。