so i made this code that asks the user about corporate's data(division's name, sales, etc)

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int SIZE = 4;
struct Division
{
    string name;
    string quarter[SIZE];
    double sales[SIZE];
};
void getData(Division []);


int main()
{
    Division corp[SIZE];
    for(int count = 0; count < SIZE; count++)
    {
        cout<<"enter division's #"<<count+1<<" name = ";
        getline(cin, corp[count].name);
    }
    cout<<endl;
    getData(corp);

    fstream data("corp.txt", ios::out | ios::binary);
    if(data)
    {
        cout<<endl<<"data has been opened"<<endl;
        data.write(reinterpret_cast<char*>(corp), sizeof(corp));
    }
    else
    {
        cout<<endl<<"error opening data!";
        return 1;
    }
}

void getData(Division CORP[])
{
    for(int count = 0; count < SIZE; count++)
    {
        cout<<"enter division "<<CORP[count].name<<" data"<<endl;
        for(int index = 0 ; index < SIZE; index++)
        {
            CORP[count].quarter[index] = index+65;
            cout<<"enter sales #"<<CORP[count].quarter[index]<<" data = ";
            cin>>CORP[count].sales[index];
        }
    }
}

it works fine, then im trying to read the file again... and stuck because i cant read the file to a struct array

#include <iostream>
#include <fstream>

using namespace std;

const int SIZE = 4;
struct Division
{
    string name;
    string quarter[SIZE];
    double sales[SIZE];
};

int main()
{
    Division corpData[SIZE];
    fstream data("corp.txt", ios::in | ios::binary);

    data.read(reinterpret_cast<char*>(corpData), sizeof(corpData));//error here (program crashes)

}

anyone can help?

note : sorry for the bad english

Recommended Answers

All 2 Replies

data.read(reinterpret_cast<char*>(corpData), sizeof(corpData));

This is an exceptionally bad idea because the Division type (of which corpData is an array) contains non-POD object types, std::string to be precise. You can't safely treat non-POD types as sequences of bytes for two very importan reasons:

  1. The structure of the object is dependent on many things, and internal "dead space", or padding, can create trap representations that will likely crash your program.

  2. Classes that manage dynamic memory internally won't be able to reproduce their structure or data simply by xcopying bytes from a file into an object that's not specifically prepared for the data. This is the most likely cause of your current crash.

I strongly recommend that you take a step back and do I/O in a simpler manner. Instead of trying to pun your data into a sequence of bytes, write and read each element of the array in a precise manner, field by field. It might be more tedious because you can't use a one-liner like read() or write(), but it's much safer.

An alternative is to support a serialize() and deserialize() process in your Division objects. This would produce a sequence of characters that can be safely read and written using the binary one-liners of read() and write().

So to summarize, these two lines are broken and need to be replaced with a different solution entirely. There's no way to make them safe as-is:

data.write(reinterpret_cast<char>(corp), sizeof(corp));
data.read(reinterpret_cast<char
>(corpData), sizeof(corpData));

commented: thx :D +0

ok thx i got it now!
i switched write and read function to some I/O lines and it works
thx again! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.