如何将ASCII和BINARY数据同时写入同一个文件 [英] How to write ASCII and BINARY data to the same file at the same time
问题描述
我需要将其更改为二进制格式,但问题是文件有一些标题(见 http://www.vtk .org / VTK / img / file-formats.pdf ),即使是二进制文件,也需要用ASCII编写。
现在我没有足够的二进制格式的经验,我第一次尝试是通过
ofstream asciiWriter(file_name.c_str())打开两个流;
ofstream binWriter(file_name.c_str(),ios :: app | ios :: binary);
问题是输出看起来杂乱无章, binWriter
没有以正确的顺序输出,所以我不能在ParaView中后处理我的文件。我试过的一件事就是每当我完成,使用 asciiWriter.flush()
和 binWriter.flush()
标题/数据写入,但这也没有帮助。
我应该怎么做?
$ b
PS:不想使用VTK包本身...它的巨大并增加了我的代码依赖!
在所有现代系统我知道,二进制和文本文件的唯一区别在于C运行时库处理换行符和文件结束符。具体而言,在* nix系统上,文本和二进制文件的行为完全相同。在Windows上,向文本文件写入'\ n'
会导致'\r'
然后将'\ n'
(LF)写入实际文件;阅读\r\\\
对显示为单个'\ n'
。在Windows上,从文本文件中读取'\x1A'
(Ctrl-Z,EOF)则表示文件结束状态。二进制文件是逐字读取和写入的,没有转换。
读取你的文档,我注意到它指定了'\\\
)在行尾。这表明,正确的方法是读取和写入的头文件,即使在Windows上的二进制文件。
'$ (code>\r\\\
作为一个无关紧要,一些旧系统(RSX-11和VMS想起来)有二进制文件,这些文件与文本文件在磁盘上非常不同。他们还直接在操作系统中支持基于记录和索引的文件。但是,他们修改了 open()
, fopen()
等函数的修改版本(以及其他语言)来处理打开文件时可能指定的过多的参数。在这样的系统上,每次都必须使用正确的文件模式。
I work with a VTK data type for my outputs. Since my data is becoming larger and larger, it's taking considerable time to write it in ASCII and that's what I have been doing so far.
I need to change that into binary format but the problem is the file has some headers (see http://www.vtk.org/VTK/img/file-formats.pdf) that need to be written in ASCII even for binary files.
Now I don't have enough experience with binary formats and my first try was to open two streams via
ofstream asciiWriter(file_name.c_str());
ofstream binWriter(file_name.c_str(), ios::app | ios::binary);
problem is the output seems to be disorganized and asciiWriter
and binWriter
are not outputting in the correct order and so I cannot post-process my file in ParaView. One thing I tried was to use asciiWriter.flush()
and binWriter.flush()
whenever I'm done with header/data writing but that does not help either.
What should I do?
PS: I do not want to use the VTK package itself ... its HUGE and adds to my code dependency!
On all modern systems that I know of, the only difference between binary and text files is the treatment of newlines and end-of-file characters by the C runtime library. Specifically, on *nix systems, text and binary files behave exactly the same. On Windows, writing a '\n'
to a text file causes a '\r'
(CR) followed by a '\n'
(LF) to be written to the actual file; reading a "\r\n"
pair shows up as a single '\n'
. Also on Windows, reading a '\x1A'
(Ctrl-Z, EOF) from a text file signals the end-of-file condition. Binary files are read and written verbatim, with no conversion.
Reading your document, I notice that it specifies '\n'
only (not "\r\n"
) at the end of the lines. That suggests that the correct approach is to read and write the header as a binary file, even on Windows.
As an irrelevant aside, some older systems (RSX-11 and VMS come to mind) had binary files that were wildly different, on disk, from text files. They also supported record-based and indexed files directly in the OS. However, they had modified versions of the open()
, fopen()
etc functions (and the equivalents in other languages) to handle the plethora of arguments that could be specified when opening a file. On such a system, you had to use the correct file mode every time.
这篇关于如何将ASCII和BINARY数据同时写入同一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!