【问题标题】:Why does the use of getline lead to "Debug assertion failed" message when setmodes are set as well?为什么在设置 setmodes 时使用 getline 会导致“调试断言失败”消息?
【发布时间】:2015-07-19 19:47:27
【问题描述】:

所以在调试下面的代码时,我会收到一个“调试断言失败”错误窗口,并显示以下消息:

Expression: ( (_Stream->_flag & _IOSTRG) || (fn = _fileno(_Stream), 
(_textmode_safe(fn) == __IOINFO_TM_ANSI) && !_tm_unicode_safe(fn))))

但是,如果我将 setmode 行注释掉,则完全没有问题。

调试总是在 getline 所在的行中断。我应该如何修改代码才能使用 getline 扫描数据?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <io.h>
#include <fcntl.h>

int _tmain(int argc, _TCHAR* argv[])
{

    _setmode(_fileno(stdin), _O_U16TEXT);
    _setmode(_fileno(stderr), _O_U16TEXT);
    _setmode(_fileno(stderr), _O_U16TEXT);

    std::string myString;
    getline(std::cin, myString);
    std::cout << "You typed in " << myString << std::endl;

    std::cin.get();
    return 0;
}

【问题讨论】:

  • 断言的哪些子表达式计算出哪个值?使用std::wcin 怎么样?删除对_setmode() 的调用怎么样?在任何情况下,您都非常了解实现定义的行为,因此请检查您正在使用的特定编译器的文档。另外,将该编译器及其版本添加到问题中。
  • 这些_setmode 是什么?你能指出文档吗?为什么不使用标准功能?
  • @lazarea:将详细信息添加到问题中。不在 cmets 部分。 问题。

标签: c++ visual-c++ getline


【解决方案1】:

既然您似乎在 STDIN 上设置了某种 UTC-16 模式,那么从 std::wcin(std::cin 的宽字符版本)读取到 std::wstring 不是更有意义吗?可以想象,这种特定于实现的非标准功能在这方面有要求。

我可以在网上找到的所有证据(不多)都表明this is the recommended approach for this mode。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-12
    • 2013-08-25
    • 2014-10-26
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多