更改std :: endl以输出CR + LF而不是LF [英] Changing std::endl to put out CR+LF instead of LF
问题描述
我在Linux平台上编写一个程序,生成可以在Windows平台上查看的文本文件。
I'm writing a program on a Linux platform that is generating text files that will be viewed on, inevitably, a Windows platform.
现在,传递 std :: endl
转换为 ostream
只生成换行符的CR字符。自然,这些文本文件在MS记事本中出现错误。
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
这样它使用CR + LF换行而不是LF? - 我知道我可以编写自己的自定义操纵器,如
win_endl
,用于生成自己的换行符,但我在很多地方使用
std :: endl
符号,像许多程序员一样,有一个倾向需要尽可能少的工作的事情。我可以简单地重载std :: endl
以产生CR + LF,或者这是一个可维护性的愚蠢的想法?
- 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
转换为适合您的平台的行结束。您的问题是新行 适合您的平台,但您创建的文件不适用于您的平台。
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 is appropriate for your platform, but the files you create aren't intended for your platform.
我不是确定你如何计划重载或改变 endl
,并改变它的行为肯定会对任何开发人员的新项目感到惊讶。我建议切换到win_endl(应该是一个简单的搜索和替换),或者从标准 ostream
切换到 Boost.Iostreams过滤流为您执行转换。
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 stream to do the conversion for you.
这篇关于更改std :: endl以输出CR + LF而不是LF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!