简体   繁体   English

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

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

So I was running a simple C++ code containing this piece所以我运行了一个简单的 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;
}

So usually the error if there is any it printed into console output but while running this code it printed this error inside the output file因此,如果有任何错误通常会打印到控制台 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.

I don't understand why?我不明白为什么? And who is Keith?基思是谁?

I know the error by the way here for(int k=i;k--;k>=1) k-- and k>=1 should be swapped我知道这里的错误for(int k=i;k--;k>=1) k-- 和 k>=1 应该交换

Whenever you are reading characters from a string or file, you have to make sure your loop ends before the string or file reaches \n or EOF .每当您从字符串或文件中读取字符时,您必须确保循环在字符串或文件到达\nEOF之前结束。

This can be verified by the compiler only if you mention bounds for the string or file which is known to compiler.仅当您提及编译器已知的字符串或文件的边界时,编译器才能验证这一点。

So instead of running the loop until k>=1 maybe you can use a while loop or maybe str.length() where str is a string variable.因此,不要在k>=1之前运行循环,也许您可以使用while循环或者str.length()其中 str 是一个字符串变量。

And maybe Keith is someone who was involved in writing the mingw compiler.也许 Keith 是参与编写 mingw 编译器的人。

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

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