首页 > 解决方案 > C ++字符串文件重定向差异?

问题描述

我正在编写一个程序,该程序通过标准输入获取输入,并根据输入执行操作。但是,与我自己的输入相比,输入的 txt 文件出现错误,我很好奇为什么。

std::string command;
std::getline(std::in, command); // first line is just "test"
if (command == "test")
{
    //works when I enter text manually through keyboard, but not from file
}

if (command.compare("test") == 1)
{
    //works reading the file
}

是什么赋予了?这在我的程序中导致了一些逻辑错误,但从我读过的 == 和 .compare 来看应该没有那么不同。

有人知道我如何解决这个可能的编码问题吗?

标签: c++stringfileio

解决方案


我修好了它。事实证明,有时从文件读取时,'\r' 字符会附加到末尾。这导致了我的错误。

希望这可以帮助其他陷入困境的人。


推荐阅读