什么std :: endl代表每个平台? [英] what does std::endl represent exactly on each platform?

查看:187
本文介绍了什么std :: endl代表每个平台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑UNIX,Windows和Mac以及输出流(二进制和文本),



std :: endl 表示,即< CR>< LF> < LF> c $ c>< CR> 吗?或者它总是相同的,无论什么平台/编译器?



我问的原因是,我编写一个TCP客户端,谈论一个协议,命令以< CR>< LF> 结尾。所以我想知道是否在我的流中使用 std :: endl \r\\\

确定,一个刷新缓冲区,另一个不刷新缓冲区。我明白了。但是如果我将文本输出到文件,则'\\\
'
等于< LF> 在Windows上是否转换为< CR>< LF> ,在Unix上是否转换为< LF> 我还没有看到清楚的答案。

解决方案

代码:

  stream<< std :: endl; 

//等价于:

stream<< \\\
< std :: flush;

所以问题是什么是\\\
映射。

在正常流没有发生。但是对于文件流(在文本模式下),\\\
被映射到从行序列的结束。注意:当读取文本模式下从文件读取时,读取将平台末尾的序列返回到'\\\
'。



因此,如果您使用正常流没有发生。如果您使用的是文件流,只需确保它以二进制模式打开,以便不应用任何转换:

  stream< ;& \r\\\
; //< CR>< LF>


Thinking about UNIX, Windows and Mac and an output stream (both binary and text),

What does std::endl represent, i.e. <CR><LF>, <LF> or <CR>? Or is it always the same no matter what platform/compiler?

The reason I'm asking is that I'm writing a TCP client that talks a protocol that expects each command to end in <CR><LF>. So I'm wondering whether to use std::endl or "\r\n" in my streams.

EDIT: Ok, so one flushes the buffer and another doesn't. I get that. But if I'm outputting text to a file, is '\n' equal to <LF> or does it convert to <CR><LF> on Windows and <LF> on Unix or not? I don't see a clear answer yet.

解决方案

The code:

stream << std::endl;

// Is equivalent to:

stream << "\n" << std::flush;

So the question is what is "\n" mapped too.
On normal streams nothing happens. But for file streams (in text mode) then the "\n" gets mapped to the platfrom end of line sequence. Note: The read converts the platform end of line sequence back to a '\n' when it reads from a file in text mode.

So if you are using a normal stream nothing happens. If you are using a file stream, just make sure it is opened in binary mode so that no conversion is applied:

stream << "\r\n"; // <CR><LF>

这篇关于什么std :: endl代表每个平台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆