写入文本文件时,表示的是什么? [英] How are doubles represented when written to text files?

查看:194
本文介绍了写入文本文件时,表示的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您将多个双精度写入文件时,它们以何种格式存储?是以字节格式还是字符串格式?

When you write a number of doubles to a file, in which format are they stored? Is it in byte format or string format?

例如。给定0.00083231。它是存储与10字节,其中每个字节表示一个数字?或者它只存储为8个字节,因为double的大小是8个字节?

E.g. given 0.00083231. Is it stored with 10 bytes, where each byte represents one digit? Or is it stored as only 8 bytes, since the size of a double is 8 bytes?

假设使用的语言是C ++。

Assume that the language used is C++.

推荐答案

问题:


你写一个数字的双打到一个文件,它们存储的格式?是以字节格式还是字符串格式?

When you write a number of doubles to a file, in which format are they stored? Is it in byte format or string format?

这取决于用来写数字的函数。

It depends on which functions you use to write the numbers.

例如:

如果使用 fprintf printf ,数字将以文本形式写出,在您的示例中,将以格式 0.000832 c>%lf,将占用8个字节。您可以更改格式以更改用于写出该数字的字节数。结果输出将是人可读的形式。同样的事情,如果你使用 cout<<

If you use fprintf or printf, the number will be written out in textual form, which, in your example, will be written as 0.000832 with the format "%lf" and will take 8 bytes. You can change the format to change the number of bytes used to write out the number. The resulting output will be in human readable form. Same thing if you use cout << number;.

如果使用 fwrite ,则该数字将以二进制形成。不管数字的值如何,存储数字所需的字节数总是 sizeof(double)。结果输出将不是人类可读的。同样的事情,如果你使用 ostream :: write

If you use fwrite, the number will be written in binary form. The number of bytes necessary to store the number will always be sizeof(double) regardless of the value of the number. The resulting output will not be human readable. Same thing if you use ostream::write.

这篇关于写入文本文件时,表示的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆