简体   繁体   English

除非包含“\\n”,否则为什么我的程序会停止

[英]Why is my program stopping unless "\n" is included

int main(int argc, char *argV[]){

istream *br;
ifstream inFile;
if(argc == 2){

    inFile.open(argV[1]);

    if(inFile.is_open()){
        cout << "file opened."; //if only "file opened" has an "\n then only it will print 
        br = &inFile;     //and the program will freeze right after printing it
    }   
}
else if(argc <= 1){

    br = &cin;

}
else{

    cout << "Unrecognized commands";

}
cout << "test"; //having \n here allows the program to reach this line of code and 
cout << "test2\n"; //everything before it

Something strange is happening.一些奇怪的事情正在发生。 Unless "\\n" is included in the string nothing will print to standard out.除非字符串中包含“\\n”,否则任何内容都不会打印到标准输出。 For example.例如。 the cout << "test" & "test2\\n" at the bottom enables the program to reach those lines of code and will cout everything up to that point, eg the "file opened" line because test2 has \\n and file opened precedes it.底部的 cout << "test" & "test2\\n" 使程序能够到达那些代码行,并将计算到该点的所有内容,例如“文件打开”行,因为 test2 有 \\n 并且文件打开先于它。 If they are changed to just cout "test1" test2" the program will not output anything, including the "file opened". Additionally, if I change "file opened" to "file opened\\n" then that will print, but if the test1 and test2 do not have a \\n they will not print, since they are after the \\n in "file opened."如果将它们更改为仅 cout“test1”test2”,程序将不会输出任何内容,包括“打开的文件”。此外,如果我将“打开的文件”更改为“打开的文件\\n”,则会打印,但如果test1 和 test2 没有 \\n 它们不会打印,因为它们在“打开的文件”中的 \\n 之后。

Streams have buffering to avoid having to do large numbers of small I/O operations.流具有缓冲以避免必须执行大量的小 I/O 操作。 By default, cout is line-buffered, so an end of line flushes the buffer.默认情况下, cout是行缓冲的,因此行尾会刷新缓冲区。 You can also explicitly flush the buffer and all buffers are flushed (that is, their contents are sent to their destinations) upon normal termination.您还可以显式刷新缓冲区,并在正常终止时刷新所有缓冲区(即,它们的内容被发送到它们的目的地)。 If our program crashes or terminates abnormally, buffers will not be flushed.如果我们的程序崩溃或异常终止,缓冲区将不会被刷新。

I suspect that your evidence that the program is stopping/freezing is limited to the fact that it doesn't produce the expected output.我怀疑您的程序正在停止/冻结的证据仅限于它不产生预期输出的事实。 You could attack this problem with a source level debugger , to get a better sense of what the program is doing when it isn't printing anything.您可以使用源代码级调试器解决此问题,以便更好地了解程序在不打印任何内容时正在执行的操作。

Adding, "print statements" can also be a useful means of debugging, but you have to do it correctly (which usually requires that you include \\n ).添加, “打印语句”也可以是一种有用的调试方法,但您必须正确执行(这通常要求您包含\\n )。 Personally, I prefer to use std::cerr for debugging, one reason being that it automatically flushes each output regardless of whether or not you include the \\n .就我个人而言,我更喜欢使用std::cerr进行调试,原因之一是无论您是否包含\\n ,它都会自动刷新每个输出。 Here's an example using cerr:下面是一个使用 cerr 的例子:

using std::cerr;
cerr<<"Unrecognized commands\n";

Fundamentally though, why would you want to cout these strings without a trailing \\n ?从根本上说,虽然,你为什么要cout这些字符串没有尾随\\n \\n is the newline character. \\n换行符 Without it, all of your outputs will be run together on the same line -- without even having intervening space characters:没有它,您的所有输出都将在同一行上一起运行——甚至没有中间空格字符:

file opened.testtest2

If you want to dive deeper, here's some related reading on buffering of stdout (specifically in 'C' though): Is stdout line buffered, unbuffered or indeterminate by default?如果你想深入了解,这里有一些关于标准输出缓冲的相关阅读(特别是在“C”中): 默认情况下标准输出行是缓冲的、无缓冲的还是不确定的?

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

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