我的程序在关闭文件然后再次 运行 以从文件中读取后没有从文件中读取是什么?
what is my program not reading from file after i close it and then run it again for reading from the file?
我编写了一个程序,以自定义数据类型存储日期和时间,然后将其存储在二进制文件中。如果我在 DevC++ 5.11 中编译和 运行 程序(快捷键 F11),一切都会完美运行。只要我不关闭程序,我的程序就能够将数据存储在文件中,然后读取它。但是,如果我关闭程序然后再次打开文件进行阅读,它只会显示我输入的第一天而不会显示其余部分。请告诉我我需要做什么来解决这个问题。
这是我的 DevC++ 5.11 代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char s=' ';
struct date{
int day;
string month;
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 >> 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;
}
};
int main(){
//declaring variables
char c='y', l;
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::out | 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 >> l;
if (l == 'y')
{
//Entering time
while (c == 'y')
{
e.gettime();
filew.write((char*)&e, sizeof(e));
cout << "\nFile write operation successful.\n";
cout << "\nDo you wish to continue? :";
cin >> c;
}
}
else
cout << "\nContinue reading...\n";
//closing file
filew.close();
cout << "\nReading file...\n";
//opening file for reading
filer.open(filename.c_str(), ios::binary | ios::in);
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;
}
您的 date
结构包含一个 string
成员。你不能 write()
/read()
a string
原样,因为字符数据(通常1)存储在内存中的其他地方。
1:不考虑短字符串优化,在这种情况下,少量字符数据实际上直接存储在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
(即,保存其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;
}
我编写了一个程序,以自定义数据类型存储日期和时间,然后将其存储在二进制文件中。如果我在 DevC++ 5.11 中编译和 运行 程序(快捷键 F11),一切都会完美运行。只要我不关闭程序,我的程序就能够将数据存储在文件中,然后读取它。但是,如果我关闭程序然后再次打开文件进行阅读,它只会显示我输入的第一天而不会显示其余部分。请告诉我我需要做什么来解决这个问题。
这是我的 DevC++ 5.11 代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char s=' ';
struct date{
int day;
string month;
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 >> 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;
}
};
int main(){
//declaring variables
char c='y', l;
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::out | 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 >> l;
if (l == 'y')
{
//Entering time
while (c == 'y')
{
e.gettime();
filew.write((char*)&e, sizeof(e));
cout << "\nFile write operation successful.\n";
cout << "\nDo you wish to continue? :";
cin >> c;
}
}
else
cout << "\nContinue reading...\n";
//closing file
filew.close();
cout << "\nReading file...\n";
//opening file for reading
filer.open(filename.c_str(), ios::binary | ios::in);
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;
}
您的 date
结构包含一个 string
成员。你不能 write()
/read()
a string
原样,因为字符数据(通常1)存储在内存中的其他地方。
1:不考虑短字符串优化,在这种情况下,少量字符数据实际上直接存储在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
(即,保存其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;
}