c++ - 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 来看应该没有那么不同。
有人知道我如何解决这个可能的编码问题吗?
解决方案
我修好了它。事实证明,有时从文件读取时,'\r' 字符会附加到末尾。这导致了我的错误。
希望这可以帮助其他陷入困境的人。
推荐阅读
- bash - 在 bash 中更改 find -printf 的值
- c - 将 char 添加到字符串数组的中间
- reactjs - 将 React App 从 Codesandbox 部署到 Github Pages
- python - 处理失去精度的numpy数组
- linux - 如何在 bash 中执行命令而不记录到历史记录?
- python - 从 Flask 中的 @app.route 更新全局字典/列表
- vim - 如何围绕程序创建包装器?
- php - 使用php将动态表单输入插入mysql数据库
- c# - 如何覆盖在某些测试中使用 IWebHostBuilder.ConfigureTestServices 模拟的服务
- c++ - c++ 在实现模板化嵌套类时遇到问题