windows 更改 std::endl 以推出 CR+LF 而不是 LF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1651936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing std::endl to put out CR+LF instead of LF
提问by J. Polfer
I'm writing a program on a Linux platform that is generating text files that will be viewed on, inevitably, a Windows platform.
我正在 Linux 平台上编写一个程序,该程序生成的文本文件将不可避免地在 Windows 平台上查看。
Right now, passing std::endl
into a ostream
generates the CR character only for newlines. Naturally, these text files look wrong in MS Notepad.
现在,传入std::endl
aostream
只为换行符生成 CR 字符。当然,这些文本文件在 MS 记事本中看起来是错误的。
- Is there a way to change
std::endl
such that it uses CR+LF for newline instead of LF? - I know I could write my own custom manipulator, like
win_endl
, for generating my own newlines, but I use thestd::endl
symbol in a lot of places, and like many programmers, have a tendency to do the thing that requires the least work possible. Could I simply overloadstd::endl
to produce CR+LF, or is this a dumb idea for maintainability?
- 有没有办法改变
std::endl
它使用 CR+LF 作为换行符而不是 LF? - 我知道我可以编写自己的自定义操纵器,例如
win_endl
,用于生成我自己的换行符,但是我std::endl
在很多地方使用了该符号,并且像许多程序员一样,倾向于做需要最少工作的事情。我可以简单地重载std::endl
以产生 CR+LF,还是这对于可维护性来说是一个愚蠢的想法?
NB: I checked out this question, but it's asking about going the other way, and the accepted answer seems rather incomplete.
注意:我检查了这个问题,但它问的是走另一条路,接受的答案似乎相当不完整。
回答by ebo
std::endl
is basicly:
std::endl
基本上是:
std::cout << "\n" << std::flush;
So just use "\r\n"
instead and omit the flush. It's faster that way, too!
所以只需使用"\r\n"
并省略冲洗。这样也更快!
From the ostream header file on endl:
从 endl 上的 ostream 头文件:
This manipulator is often mistakenly used when a simple newline is desired, leading to poor buffering performance.
当需要简单的换行符时,经常会错误地使用此操纵器,从而导致缓冲性能不佳。
回答by Josh Kelley
Opening a file in text mode should cause std::endl
to be converted to the appropriate line ending for your platform. Your problem is that newline isappropriate for your platform, but the files you create aren't intended for your platform.
在文本模式下打开文件应该会导致std::endl
转换为适合您平台的行尾。你的问题是,换行符是适合你的平台,但您所创建的文件不适用于您的平台。
I'm not sure how you plan on overloading or changing endl
, and changing its behavior would certainly be surprising for any developers new to your project. I'd recommend switching to win_endl (should be a simple search-and-replace) or maybe switching from a standard ostream
to a Boost.Iostreams filtering streamto do the conversion for you.
我不确定您打算如何重载或更改endl
,并且更改其行为对于您的项目的任何新开发人员来说肯定会令人惊讶。我建议切换到 win_endl(应该是一个简单的搜索和替换),或者从标准切换ostream
到Boost.Iostreams 过滤流来为您进行转换。
回答by Greg Hewgill
Windows Notepad is pretty much the onlyWindows program you'll find that doesn't handle LF-only files properly. Almost everything else (including WordPad) handles LF-only files just fine.
Windows记事本几乎是唯一的Windows程序,你会发现,不处理LF-文件只正常。几乎所有其他东西(包括写字板)都可以很好地处理 LF-only 文件。
This problem is a bug in Notepad.
此问题是记事本中的错误。
回答by zumalifeguard
You shouldn't use \r\n. Just use \n, but then open the stream in "text" mode, which than will do the conversion for you. You may not care about cross-platform, but this is the official way of doing it.
你不应该使用\r\n。只需使用\n,然后以“文本”模式打开流,这将为您进行转换。您可能不关心跨平台,但这是官方的做法。
That way, the same code will spit-out \n on unix, \r\n on windows, and \r on mac.
这样,相同的代码将在 unix 上输出 \n,在 Windows 上输出 \r\n,在 mac 上输出 \r。
回答by J. Polfer
Here was my solution to the problem. It's a bit of a mash-up of all the info provided in the answers:
这是我对问题的解决方案。这是答案中提供的所有信息的混搭:
I created a macro in a win_endl.h file for the newline I wanted:
#define win_endl "\r\n"
Then I did a search-and-replace:
sheepsimulator@sheep\_machine > sed -i 's/std::endl/win_endl' *
我在 win_endl.h 文件中为我想要的换行符创建了一个宏:
#define win_endl "\r\n"
然后我做了一个搜索和替换:
sheepsimulator@sheep\_machine > sed -i 's/std::endl/win_endl' *
And ensured all my files included win_endl.h
.
并确保我的所有文件都包含在内win_endl.h
。