嘿伙计们,我正在使用二进制文件处理这个简单的银行帐户程序。 由于一些奇怪的原因,我无法从文件中读取。或者更重要的是,只要我尝试从文件中读取,程序就会崩溃。
有人能指出我正确的方向吗?谢谢。
Main.cpp的
#include <iostream>
#include<fstream>
#include<cstdlib>
#include "Account_Querry.h"
using namespace std;
int main()
{
Account_Querry Account;
int choice;
cout<<"***Acount Information System***"<<endl;
while(true)
{
cout<<"Select one option below ";
cout<<"\n\t1-->Add record to file";
cout<<"\n\t2-->Show All records in file";
cout<<"\n\t3-->Search Record from file";
cout<<"\n\t4-->Update Record";
cout<<"\n\t5-->Delete Record";
cout<<"\n\t6-->Quit";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
Account.write_rec();
break;
case 2:
//Account.search_rec();
break;
case 3:
Account.read_rec();
break;
case 4:
//Account.edit_rec();
break;
case 5:
// Account.delete_rec();
break;
case 6:
exit(0);
break;
default:
cout<<"\nEnter corret choice" << endl;
}
}
system("pause");
return 0;
}
Account_Querry.h
#ifndef ACCOUNT_QUERRY_H
#define ACCOUNT_QUERRY_H
#include <string>
#include <fstream>
class Account_Querry
{
private:
int accountNo;
std::string firstName;
std::string lastName;
double balance;
public:
void setAccountNo(int accountNo);
int getAccountNo();
void setFirstName(std::string firstName);
std::string getFirstName();
void setLastName(std::string lastName);
std::string getLastName();
void setBalance(double balance);
double getBalance();
void read_data();
void show_data();
void write_rec();
void read_rec();
void search_rec();
void edit_rec();
void delete_rec();
};
#endif // ACCOUNT_QUERRY_H
Account_Querry.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <math.h>
#include "Account_Querry.h"
using namespace std;
void Account_Querry::setAccountNo(int accountNo)
{
this->accountNo = accountNo;
}
int Account_Querry::getAccountNo()
{
return accountNo;
}
void Account_Querry::setFirstName(string firstName)
{
this->firstName = firstName;
}
string Account_Querry::getFirstName()
{
return firstName;
}
void Account_Querry::setLastName(string lastName)
{
this->lastName = lastName;
}
string Account_Querry::getLastName()
{
return lastName;
}
void Account_Querry::setBalance(double balance)
{
this->balance = balance;
}
double Account_Querry::getBalance()
{
return balance;
}
void Account_Querry::show_data()
{
cout << "Current Information:" << endl;
cout << "-------------------------------------------------" << endl;
cout << "Name: " << firstName << ' ' << lastName << endl;
cout << "Balance: " << balance << endl;
cout << "ID#: " << accountNo << endl;
cout << "-------------------------------------------------" << endl;
cout << endl;
}
void Account_Querry::read_data()
{
cout<<"\nEnter Account Number: ";
cin>>accountNo;
cin.ignore();
cout<<"Enter First Name: ";
getline(cin,firstName,'\n');
cout<<"Enter Last Name: ";
getline(cin,lastName,'\n');
cout<<"Enter Balance: ";
cin>>balance;
}
void Account_Querry::write_rec()
{
ofstream outfile("record.bank", ofstream::app);
read_data();
outfile.write(reinterpret_cast<char *>(this), sizeof(*this));
outfile.close();
system("cls");
}
void Account_Querry::read_rec()
{
ifstream infile;
infile.open("record.bank", ios::binary);
if(!infile)
{
cout<<"Error in Opening! File Not Found!!"<<endl;
return;
}
cout<<"\n****Data from file****"<<endl;
while(!infile.eof())
{
if(infile.read(reinterpret_cast<char*>(this), sizeof(*this))>0)
{
show_data();
}
}
infile.close();
}
答案 0 :(得分:1)
首先,感谢发布整个代码。
我刚刚在Windows中创建了一个空的Visual Studio项目并创建了3个文件并复制了您在问题中发布的代码。
代码似乎执行得很好,但在执行时,Option 2
不会显示文件中的所有记录。 Option 3
确实显示文件中的所有记录。您可以更新选项部分。
除此之外,我编译为创建32位二进制文件和64位二进制文件,它们都似乎工作正常。 Option 1
正在添加新记录,Option 3
正在打印所有记录。如果我在添加一条记录(删除bank.record文件)之前尝试执行Option 3
(显示所有记录),它也会打印错误消息。
你能告诉你如何编译代码(VisualStudio / gcc /..)在哪个操作系统上(Windows / Ubuntu /..)?
您执行时还提供了哪些输入。您是否输入任何非ASCII字符名称,如某些日文字符?
一个建议是:不将类对象转储到二进制文件(如write_rec
中所做)。 更好地创建具有固定大小变量的结构(不使用任何字符串/向量)。
例如,代替字符串使用字符数组,而不是矢量使用静态数组等。这将避免很多回读问题。
希望这有助于解决您的问题。