juanmindset
juanmindset

Reputation: 1

Output a double written in a binary file C++

I have a class named "obj" that has two data types one int and one double. Im trying to just read in the third object but cant seem to figure it out. It was working before I changed one of the data type to double. I feel like it has to do with the typecast. TO sum up I cant get it to output only the third object after the file has been wrriten. Any suggestions?

#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();





    }

Upvotes: 0

Views: 230

Answers (1)

freakish
freakish

Reputation: 56557

The problem is that sizeof(Data) is not the sum of sizeof(int) + sizeof(double) thus seekfile.seekg(2 * sizeof(Data), ios::beg) is incorrect. This

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

should fix the problem.

Note that sizeof(Data) is greater then the sum of its components due to padding. See this for more info:

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

Also note that if you redefine num2 to int then there is no need for padding and in that case your original code works.

Upvotes: 0

Related Questions