0

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





    }
  • Suggestions: 1) Let us know exactly what is not working. Is it a compile error, runtime error or unexpected results? 2) Ask a better question than "Any suggestions?" – 463035818_is_not_an_ai Oct 03 '16 at 20:39
  • Its not ouputting the third object only giving me what i believe is a memory space – juanmindset Oct 03 '16 at 20:40
  • What's the definition of `Data`? – Peter Oct 03 '16 at 20:43
  • 5
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Oct 03 '16 at 20:43
  • Please read about [mcve]. Your code as you posted it cant be compiled by others and there might be errors in code that you dont show here. Please dont post all the code!! Read the link and try to reduce the code to the minimum needed to demonstrate the issue – 463035818_is_not_an_ai Oct 03 '16 at 20:44
  • Data is the class sorry accidentally put "obj" in the question – juanmindset Oct 03 '16 at 20:45
  • added the whole code and yes it does compile only problem is what I described I cannot output only the third object – juanmindset Oct 03 '16 at 20:46
  • A MCVE would also make it easier to see that you left your last name in one of the filenames, which you presumably did not intend – Peter Oct 03 '16 at 20:46
  • @juanmindset There reason it fails is because `sizeof(Data)` **is not** the sum of `sizeof(int)` + `sizeof(double)` while it is true for 2 ints, i.e. this `seekfile.seekg(2 * sizeof(Data), ios::beg);` is wrong. – freakish Oct 03 '16 at 20:49
  • In your case an MCVE might be, for example, just the write and the seek/read, with no user input, and a simple datatype instead of a compound type. – Peter Oct 03 '16 at 20:49
  • @freakish thats what I figured I tried sizeof(obj[1]) + sizeof(obj[2]) but it still produced the same results – juanmindset Oct 03 '16 at 20:50
  • @juanmindset You need `seekfile.seekg(2 * (sizeof(third.num1) + sizeof(third.num2)), ios::beg);` Let me know if it works. – freakish Oct 03 '16 at 20:51
  • @freakish still nothing it keeps outputting -858993460 -9.25596e+061 – juanmindset Oct 03 '16 at 20:53
  • @juanmindset Are you sure you have brackets correctly in `2 * (sizeof(third.num1) + sizeof(third.num2))`? If it doesn't work then I'm afraid I can't help you. But I'm pretty sure it is related to incorrect `seekg` call. – freakish Oct 03 '16 at 21:04
  • @freakish yeah I did thanks for trying though appreciate it – juanmindset Oct 03 '16 at 21:11
  • @freakish ok I solved it what you told me did work compiler just wasnt updating the code for some reason – juanmindset Oct 03 '16 at 21:15

1 Answers1

0

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.

Community
  • 1
  • 1
freakish
  • 54,167
  • 9
  • 132
  • 169