【问题标题】:Output a double written in a binary file C++输出用二进制文件 C++ 编写的双精度
【发布时间】:2016-10-03 20:38:11
【问题描述】:

我有一个名为“obj”的类,它有两种数据类型,一种是 int,一种是 double。我试图只阅读第三个对象,但似乎无法弄清楚。在我将其中一种数据类型更改为加倍之前,它正在工作。我觉得这与类型转换有关。总而言之,在写入文件后,我无法让它只输出第三个对象。有什么建议吗?

#include<iostream>
#include<fstream>

using namespace std;

class Data {

public:
    int num1;
    double num2;

    Data() {}
    ~Data() {}
    void setData();
    void getData();
};

void Data::getData()
{
    cout << "Enter first number: ";
    cin >> num1;

    cout << "Eneter second number: ";
    cin >> num2;

}









#include "class.h"


    const int SIZE = 5;
    int main()
    {

        Data obj[SIZE];

        for (int i = 0; i < SIZE; i++)
        {
            cout << "Enter numbers of object " << i+1 << endl;

            obj[i].getData();

        }


        ofstream outFile;
        outFile.open("C:\\Users\\juan\\Desktop\\26.bin", ios::out | ios::binary);

        for (int i = 0; i < SIZE; i++)
        {

            outFile.write(reinterpret_cast<char *> (&obj[i].num1), sizeof(obj[i].num1));
            outFile.write(reinterpret_cast<char *> (&obj[i].num2), sizeof(obj[i].num2));

        }

        cout << "Writing to file...." << endl;

        outFile.close();

        ifstream inFile;
        inFile.open("C:\\Users\\juan\\Desktop\\26.bin", ios::in | ios::binary);

        for (int i = 0; i < SIZE; i++)
        {
            inFile.read(reinterpret_cast<char *> (&obj[i].num1), sizeof(obj[i].num1));
            inFile.read(reinterpret_cast<char *> (&obj[i].num2), sizeof(obj[i].num2));



        }

        for (int i = 0; i < SIZE; i++)
        {
            cout << obj[i].num1 << endl;
            cout << obj[i].num2 << endl;
            cout << endl << endl;
        }

        inFile.close();

        Data third;
        fstream seekfile;


        seekfile.open("C:\\Users\\juan\\Desktop\\26.bin", ios::in | ios::binary);

        seekfile.seekg(2 * sizeof(Data), ios::beg);

        seekfile.read(reinterpret_cast<char *> (&third.num1), sizeof(third.num1));
        seekfile.read(reinterpret_cast<char *> (&third.num2), sizeof(third.num2));

        cout << endl << endl;
        cout << third.num1 << endl;
        cout << third.num2 << endl;

        seekfile.close();





    }

【问题讨论】:

  • 建议: 1) 让我们确切地知道什么不起作用。是编译错误、运行时错误还是意外结果? 2) 问一个比“有什么建议吗?”更好的问题
  • 它没有输出第三个对象,只给了我我认为是内存空间的东西
  • Data的定义是什么?
  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 请阅读minimal reproducible example。您发布的代码不能被其他人编译,并且代码中可能存在您未在此处显示的错误。请不要发布所有代码!阅读链接并尝试将代码减少到演示问题所需的最低限度

标签: c++ file fstream bin seekg


【解决方案1】:

问题在于sizeof(Data) 不是sizeof(int) + sizeof(double) 的总和,因此seekfile.seekg(2 * sizeof(Data), ios::beg) 不正确。这个

seekfile.seekg(2 * (sizeof(third.num1) + sizeof(third.num2))

应该解决问题。

请注意,sizeof(Data) 由于填充而大于其组件的总和。有关详细信息,请参阅此内容:

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

另请注意,如果您将 num2 重新定义为 int,则无需填充,在这种情况下,您的原始代码可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多