二进制文件打印和所需的精度
我正在将变量 z1 打印到文本文件中,它是一个包含浮点数的一维数组,以便我可以导入到 Matlab 或 GNUPlot 中进行绘图。我听说二进制文件 (.dat) 比 .txt 文件小。我当前用于打印到 .txt 文件的定义是:
void create_out_file(const char *file_name, const long double *z1, size_t z_size){
FILE *out;
size_t i;
if((out = _fsopen(file_name, "w+", _SH_DENYWR)) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < z_size; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
我有三个问题:
二进制文件真的比文本文件更紧凑吗?
如果是,我想知道如何修改上面的代码,以便可以将数组 z1 的值打印到二进制文件中。我读到 fprintf 必须替换为 fwrite。我的输出文件说 dodo.dat 应该包含数组 z1 的值,每行一个浮点数。
我的代码中有 %.16Le,但我认为 %.15Le 是正确的,因为我有 15 个长双精度数字。我在宽度位置放置了一个点 (.),因为我相信这允许扩展到任意字段以保存所需的数字。我说得对吗?以 %.16Le 为例,我可以得到类似 1.0047914240730432e-002 的输出,它为我提供 16 个精度数字,并且字段的宽度具有正确显示数字的正确宽度。在宽度位置放置点 (.) 而不是宽度值是一种好的做法吗?
非常感谢...
是否更改为:
for(i = 0; i < z_size; i++)
fwrite(&z1, sizeof(long double), 1, out);
更新除了更改为“wb+”之外, ok?我无法在 Matlab 中读取二进制文件。
I'm printing a variable say z1 which is a 1-D array containing floating point numbers to a text file so that I can import into Matlab or GNUPlot for plotting. I've heard that binary files (.dat) are smaller than .txt files. The definition that I currently use for printing to a .txt file is:
void create_out_file(const char *file_name, const long double *z1, size_t z_size){
FILE *out;
size_t i;
if((out = _fsopen(file_name, "w+", _SH_DENYWR)) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < z_size; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
I have three questions:
Are binary files really more compact than text files?;
If yes, I would like to know how to modify the above code so that I can print the values of the array z1 to a binary file. I've read that fprintf has to be replaced with fwrite. My output file say dodo.dat should contain the values of array z1 with one floating number per line.
I have %.16Le up in my code but I think that %.15Le is right as I have 15 precision digits with long double. I have put a dot (.) in the width position as I believe that this allows expansion to an arbitrary field to hold the desired number. Am I right? As an example with %.16Le, I can have an output like 1.0047914240730432e-002 which gives me 16 precision digits and the width of the field has the right width to display the number correctly. Is placing a dot (.) in the width position instead of a width value a good practice?
Thanks a lot...
UPDATE Is changing to:
for(i = 0; i < z_size; i++)
fwrite(&z1, sizeof(long double), 1, out);
ok in addition to the change to "wb+" ? I can't read the binary file in Matlab.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,二进制文件更紧凑,但是您会失去可移植性,并且还存在各种其他潜在问题,因此除非您的数据文件太大或导出/导入速度慢,否则最好坚持使用文本,如果您可以(您始终可以压缩它们以进行存储,例如使用 zip)
使用“wb”而不是“w”打开文件并使用
fwrite ()
- 文件中不再有“行” - 它只是一个(二进制)浮点值流您可能会对
double
和long double
-long double
的大小最多可达 16 个字节,精度最高可达 32 位左右(但这取决于实现 - long double通常可以是 10、12 或 16 字节)。double
通常为 8 个字节,精度约为 16 位数字。MATLAB 可能无法处理
long double
(因为它没有很好地标准化),因此您可能只想将双精度数写入数据文件,例如yes, binary files are more compact, but you lose portability and there are various other potential problems too, so unless your data files are problematically huge, or slow to export/import, it's a good idea to stick with text if you can (you can always compress them for storage, e.g. with zip)
open you file with "wb" instead of "w" and use
fwrite()
- you no longer have "lines" in your file - it will just be a stream of (binary) floating point valuesyou may be getting confused between
double
andlong double
- along double
can be up to 16 bytes in size and have a precision of up to around 32 digits (however this is implementation-dependent - long double can commonly be 10, 12 or 16 bytes). Adouble
is usually 8 bytes and has a precision of around 16 digits.MATLAB may not be able to cope with
long double
(as it is not well standardized) so you probably just want to write doubles to your data file, e.g.