繁体   English   中英

为什么 output 不在控制台中打印 output C++

[英]Why output is printed not in the console output C++

所以我运行了一个简单的 C++ 代码,其中包含这部分

freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
for(int k=i;k--;k>=1)
{
    if(outgoing[k]=='Y' && incoming[k-1]=='Y')
        result[i][k-1]='Y';
    else
        break;
}

因此,如果有任何错误通常会打印到控制台 output 但是在运行此代码时,它会在 output 文件中打印此错误

/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067:
 std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference
 std::__cxx11::basic_string<_CharT, _Traits,
 _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>;
 std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference =
 char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type
 = unsigned int]: Assertion '__pos <= size()' failed.

我不明白为什么? 基思是谁?

我知道这里的错误for(int k=i;k--;k>=1) k-- 和 k>=1 应该交换

每当您从字符串或文件中读取字符时,您必须确保循环在字符串或文件到达\nEOF之前结束。

仅当您提及编译器已知的字符串或文件的边界时,编译器才能验证这一点。

因此,不要在k>=1之前运行循环,也许您可以使用while循环或者str.length()其中 str 是一个字符串变量。

也许 Keith 是参与编写 mingw 编译器的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM