C++ 将 double 类型值写入文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6226210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:43:14  来源:igfitidea点击:

Writing a double type value to a text file

c++pointersfile-io

提问by Aquarius_Girl

The below code is writing unreadable characters to the text file:

以下代码将不可读的字符写入文本文件:

int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;
        char   *conversion = (char *)&value;
        strcat (conversion, "
int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;

        myfile << value;
        myfile.close();
    }   

    return 0;
}
"); myfile.write (conversion, strlen (conversion)); myfile.close(); } return 0; }

I want to see the actual number written in the file :( Hints please.

我想查看文件中写入的实际数字:( 请提示。

EDITSeeing the answers below, I modified the code as:

编辑看到下面的答案,我将代码修改为:

 #include <iomanip>

 myfile << std::fixed << std::setprecision(8) << value;
 myfile.close();

This produces the putput: 11.2344while the actual number is 11.23444556. I want the complete number.

这会产生 putput:11.2344而实际数字是11.23444556。我想要完整的数字。

Editing the post to notify everyone:The unreadable characters are due to ofstream's write function:

编辑帖子通知大家:不可读的字符是由于ofstream的写功能:

This is an unformatted output function

这是一个未格式化的输出函数

This quote is from: http://www.cplusplus.com/reference/iostream/ostream/write/

此引文来自:http: //www.cplusplus.com/reference/iostream/ostream/write/

回答by Nawaz

Why don't you simply do this (updated answer after the edit in the question):

你为什么不简单地这样做(在问题中编辑后更新答案):

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main () 
{
    ofstream myfile ("example.txt");

    if (myfile.is_open())
    {
        double value       = 11.23444556;

        myfile << value;
        myfile.close();
    }   

    return 0;
}

Now, you can see the actual number written in the file.

现在,您可以看到文件中写入的实际数字。

See the documentation of std::setprecision. Note: you have to include the <iomanip>header file.

请参阅 的文档std::setprecision。注意:您必须包含<iomanip>头文件。

回答by MGwynne

It's easier here to use the stream operators:

在这里使用流运算符更容易:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

int main () 
{
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    double value       = 11.23444556;
    char     *conversion = reinterpret_cast<char *>(&value);
    strcat (conversion, "
myfile << value;
"); //myfile.write (*conversion, strlen (conversion)); myfile << *(reinterpret_cast<double *>(conversion)); myfile.close(); } return 0; }

gives you what you want.

给你你想要的。

回答by Umut Tabak

Others suggested better ways but if you really want to do it the pointer way there should be casts to convert char* to double * and vice versa

其他人建议了更好的方法,但如果你真的想这样做,指针的方式应该转换为 char* 到 double *,反之亦然

##代码##

回答by maxschlepzig

You can write the value to file like this:

您可以将值写入文件,如下所示:

##代码##

Using the adress of value as parameter strcatcorrupts your stack, since strcatexpects a 0-terminated c-string as first argument (and actually allocated space).

使用值的地址作为参数strcat会破坏您的堆栈,因为strcat期望以 0 结尾的 c 字符串作为第一个参数(和实际分配的空间)。