Ваша структура date
содержит string
член. Вы не можете write()
/ read()
a string
как есть, поскольку символьные данные (обычно 1 ) хранятся в другом месте в памяти.
1: не принимая во внимание Short String Optimization, в этом случае небольшие объемы символьных данных фактически сохраняются непосредственно в объекте string
.
Используйте фиксированный массив char[]
вместо string
, Например:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct date{
int day;
char month[10];
int year;
};
struct time{
int hr;
int min;
};
class entry{
private:
int sno;
date d;
time t;
public:
void initsno(){
sno=0;
}
void getdate(){
cout << "\nEnter the date : ";
cout << "\nDay : ";
cin >> d.day;
cout << "Month : ";
cin.get(d.month, sizeof(d.month));
cout << "Year :";
cin >> d.year;
}
void gettime(){
cout << "\n\nEnter time : ";
cout << "\nHours :";
cin >> t.hr;
cout << "Minutes :";
cin >> t.min;
}
void showdate(){
cout << "\n\nDate is : ";
cout << d.day << s << d.month << s << d.year << endl << endl;
}
void showtime(){
cout << "\n\nTime is :";
cout << t.hr << ":" << t.min << endl << endl;
}
};
int main(){
//declaring variables
char c;
string filename;
entry e, e2;
ofstream filew;
ifstream filer;
cout << "******Time sheet calculator******" << endl << endl;
//getting file name begins
cout << "Enter the name of file you want to create : ";
cin >> filename;
cout << "\nThe file name is : " << filename << endl;
//getting file name over
e.initsno(); //initializing the entry serial number
//creating and opening file
filew.open(filename.c_str(), ios::binary | ios::app);
//file association operation successful
if (!filew)
cout << "\nCan't open file" << endl;
else
cout << "continue write operation\n";
//getting date
e.getdate();
cout << "\nDo you want to write to file? :";
cin >> c;
if (c == 'y')
{
//Entering time
do
{
e.gettime();
filew.write((char*)&e, sizeof(e));
cout << "\nFile write operation successful.\n";
cout << "\nDo you wish to continue? :";
cin >> c;
}
while (c == 'y');
}
else
cout << "\nContinue reading...\n";
//closing file
filew.close();
cout << "\nReading file...\n";
//opening file for reading
filer.open(filename.c_str(), ios::binary);
if (!filer)
cout << "\nCan't open file" << endl;
else
cout << "continue read operation\n";
while (filer.read((char*)&e2, sizeof(e2)))
{
cout << "\nDisplaying file data...\n";
e2.showdate();
e2.showtime();
}
cout << "\n\nFile IO successful...";
filer.close();
return 0;
}
В противном случае вам необходимо сериализовать string
(ie, сохранить его значение size()
с последующими фактическими символьными данными), например:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct date{
int day;
string month;
int year;
void read(istream &is) {
is.read(reinterpret_cast<char*>(&day), sizeof(day));
size_t size;
if (is.read(reinterpret_cast<char*>(&size), sizeof(size)))
{
month.resize(size);
is.read(&month[0], size);
}
is.read(reinterpret_cast<char*>(&year), sizeof(year));
}
void write(ostream &os) const {
os.write(reinterpret_cast<const char*>(&day), sizeof(day));
size_t size = month.size();
os.write(reinterpret_cast<const char*>(&size), sizeof(size));
os.write(month.c_str(), size);
os.write(reinterpret_cast<const char*>(&year), sizeof(year));
}
};
istream& operator>>(istream &is, date &d)
{
d.read(is);
return is;
}
ostream& operator<<(ostream &os, const date &d)
{
d.write(os);
return os;
}
struct time{
int hr;
int min;
void read(istream &is)
{
is.read(reinterpret_cast<char*>(&hr), sizeof(hr));
is.read(reinterpret_cast<char*>(&min), sizeof(min));
}
void write(ostream &os) const
{
os.write(reinterpret_cast<const char*>(&hr), sizeof(hr));
os.write(reinterpret_cast<const char*>(&min), sizeof(min));
}
};
istream& operator>>(istream &is, time &t)
{
t.read(is);
return is;
}
ostream& operator<<(ostream &os, const time &t)
{
t.write(os);
return os;
}
class entry{
private:
int sno;
date d;
time t;
public:
void initsno(){
sno=0;
}
void getdate(){
cout << "\nEnter the date : ";
cout << "\nDay : ";
cin >> d.day;
cout << "Month : ";
cin >> d.month;
cout << "Yeay :";
cin >> d.year;
}
void gettime(){
cout << "\n\nEnter time : ";
cout << "\nHours :";
cin >> t.hr;
cout << "Minutes :";
cin >> t.min;
}
void showdate(){
cout << "\n\nDate is : ";
cout << d.day << s << d.month << s << d.year << endl << endl;
}
void showtime(){
cout << "\n\nTime is :";
cout << t.hr << ":" << t.min << endl << endl;
}
void read(istream &is) {
is.read(reinterpret_cast<char*>(&sno), sizeof(sno));
d.read(is);
t.read(is);
}
void write(ostream &os) const {
os.write(reinterpret_cast<const char*>(&sno), sizeof(sno));
d.write(os);
t.write(os);
}
};
istream& operator>>(istream &is, entry &e)
{
e.read(is);
return is;
}
ostream& operator<<(ostream &os, const entry &e)
{
e.write(os);
return os;
}
int main(){
//declaring variables
char c;
string filename;
entry e, e2;
ofstream filew;
ifstream filer;
cout << "******Time sheet calculator******" << endl << endl;
//getting file name begins
cout << "Enter the name of file you want to create : ";
cin >> filename;
cout << "\nThe file name is : " << filename << endl;
//getting file name over
e.initsno(); //initializing the entry serial number
//creating and opening file
filew.open(filename.c_str(), ios::binary | ios::app);
//file association operation successful
if (!filew)
cout << "\nCan't open file" << endl;
else
cout << "continue write operation\n";
//getting date
e.getdate();
cout << "\nDo you want to write to file? :";
cin >> c;
if (c == 'y')
{
//Entering time
do
{
e.gettime();
filew << e;
cout << "\nFile write operation successful.\n";
cout << "\nDo you wish to continue? :";
cin >> c;
}
while (c == 'y');
}
else
cout << "\nContinue reading...\n";
//closing file
filew.close();
cout << "\nReading file...\n";
//opening file for reading
filer.open(filename.c_str(), ios::binary);
if (!filer)
cout << "\nCan't open file" << endl;
else
cout << "continue read operation\n";
while (filer >> e2)
{
cout << "\nDisplaying file data...\n";
e2.showdate();
e2.showtime();
}
cout << "\n\nFile IO successful...";
filer.close();
return 0;
}