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