简体   繁体   English

cout部分打印而没有endl

[英]cout partially print without endl

I'm printing a bunch of strings as following: 我正在打印一堆字符串,如下所示:

cout<<count<<"|"<<newTime.time<<"|"<<newCat<<"|"<<newCon<<endl;

in which count is a counter, newTime.time is a string of time, and newCat and newCon are both strings. 其中count是一个计数器,newTime.time是一个时间字符串,而newCat和newCon都是字符串。 The output is like following: 输出如下:

06:02:11:20:08|DB Mgr|Sending query: “SELECT * FROM users” 

Apparently, it left out the count and "|". 显然,它省略了计数和“ |”。 However, if I change the code into 但是,如果我将代码更改为

cout<<count<<"|"<<endl;
cout<<newTime.time<<"|"<<newCat<<"|"<<newCon<<endl;

The output just turned into 输出刚变成

2|
06:02:11:20:08|DB Mgr|Sending query: “SELECT * FROM users” 

I was first thinking if this is the problem of buffer. 我首先想到的是这是否是缓冲区的问题。 I changed endl to flush but the problem still exists. 我将endl更改为冲洗,但问题仍然存在。 Thanks for any help. 谢谢你的帮助。

It sounds like your time string may have a carriage return \\r in it. 听起来您的time字符串中可能包含回车符\\r If that's the case, then outputting using your first method will still output the count and separator, but the \\r will return to the start of the line and begin overwriting it. 如果是这种情况,那么使用您的第一种方法进行输出仍然会输出计数和分隔符,但是\\r将返回到行的开头并开始覆盖它。

Your second method will not overwrite the count since it's on the previous line (a \\r will have little visible effect if you're already at the start of the line). 第二种方法不会覆盖计数,因为它位于前一行(如果您已经在该行的开头, \\r几乎看不到效果)。

If you're running on a UNIX-like platform, you can pipe the output through something like od -xcb (a hex dump filter) to see if there is a \\r in the output. 如果您在类似UNIX的平台上运行,则可以通过od -xcb (十六进制转储过滤器)之类的内容通过管道传递输出,以查看输出中是否存在\\r

Alternatively, if you have a string in your code, you can see if it contains a carriage return with something like: 另外,如果您的代码中有一个字符串,则可以查看它是否包含带有以下内容的回车符:

std::string s = "whatever";
size_t pos = s.find ('\r');
if (pos != std::string::npos) {
    // carriage return was found.
}

By way of example, the following program: 例如,以下程序:

#include <iostream>

int main (void) {
    std::string s1 = "strA";
    std::string s2 = "\rstrB";
    std::string s3 = "strC";

    std::cout << s1 << '|' << s2 << '|' << s3 << '\n';
    std::cout << "=====\n";

    std::cout << s1 << '|' << '\n';
    std::cout << s2 << '|' << s3 << '\n';

    std::cout << "=====\n";
    size_t pos = s2.find ('\r');
    if (pos != std::string::npos)
        std::cout << "CR found at " << pos << '\n';
    return 0;
}

seems to output the following: 似乎输出以下内容:

strB|strC
=====
strA|
strB|strC
=====
CR found at 0

but in fact that first line is actually: 但实际上第一行实际上是:

strA|(\r)strB|strC

where (\\r) is the carriage return. 其中(\\r)是回车符。


And keep in mind you rarely need endl - it's effectively a \\n with a flush which is not really necessary in most cases. 并且请记住,您很少需要endl实际上,它是带有\\n ,在大多数情况下并不是必须的。 You can just get away with using \\n and let the automated flushing take care of itself. 您可以使用\\n摆脱\\n ,让自动冲洗功能自理。

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

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