简体   繁体   English

C ++ ofstream,无需CRLF即可打印

[英]C++ ofstream, printing without CRLF

I have a C++ code I am running in Linux with wine. 我有一个C ++代码,我正在用wine在Linux中运行。 I think this is actually part of the problem. 我认为这实际上是问题的一部分。

Usually, when I do something like this in a native Linux C++ program: 通常,当我在本地Linux C ++程序中执行以下操作时:

ofstream fout;
fout.open("myfile.txt")
fout<<"blah blah"<<endl;
fout<<"blah blah 2"<<endl;
fout.close;

The file is standard ASCII text. 该文件是标准ASCII文本。 However, in the code I an running under wine, myfile.txt is now ASCII text with CRLF line terminators. 但是,在我在wine下运行的代码中,myfile.txt现在是带有CRLF行终止符的ASCII文本。

This is a problem because if I want to read the file using a native Linux C++ code running on the same machine, the CRLF line terminators really mess up a lot of the file handling and parsing. 这是一个问题,因为如果我想使用在同一台计算机上运行的本机Linux C ++代码读取文件,则CRLF行终止符确实会弄乱很多文件处理和解析。

Is there a way to get the code running under wine to output files without CRLF line terminators and in a fashion that I can read it using the native Linux C++ code on the same machine? 有没有一种方法可以让代码在wine下运行,以输出没有CRLF行终止符的文件,并且可以在同一台计算机上使用本机Linux C ++代码读取它的方式?

You could open the file in ios::binary mode. 您可以在ios::binary模式下打开文件。 This doesn't, strictly speaking, mean that it's a binary file [any more than any other file is "text", since all files are binary]. 严格来讲,这并不意味着它是一个二进制文件(比所有其他文件都是“文本”,因为所有文件都是二进制文件)。 Binary in this context just means "don't muck about with the stuff inside the file by interpreting characters as special, add or remove any characters, etc. 在这种情况下,二进制仅表示“不要通过将字符解释为特殊字符,添加或删除任何字符等来弄乱文件中的内容。

Or when you copy the file to Linux, use dos2unix myfile.txt to convert it from "dos" (and Windows) format to "unix" style text file. 或者,当您将文件复制到Linux时,请使用dos2unix myfile.txt将其从“ dos”(和Windows)格式转换为“ unix”样式的文本文件。

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

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