我正在制作的这部分程序应该打印出存储在随机访问二进制文件中的所有记录。每次我运行它时,它都会打印出所有记录,然后抛出访问冲突异常:
void readFile()
{
ifstream inFile("hardware.txt", ios::in | ios::binary);
if(!inFile)
{
cerr<<"File could not be opened."<<endl;
exit(1);
}
cout<<left<<setw(10)<<"Account"<<setw(16)
<<"Name"<<setw(11)<<"Quantity"<<left<<setw(10)<<"Price"<<endl;
Hardware File;
inFile.read(reinterpret_cast<char *>(&File), sizeof(Hardware));
while(inFile && !inFile.eof())
{
if(File.getAccountNumber() != 0)
outputLine(cout, File);
inFile.read(reinterpret_cast<char *>(&File), sizeof(Hardware));
}
}
澄清一下,“硬件”是存储在 Hardware.h 中的一个类。
硬件.h
#ifndef HARDWARE_H
#define HARDWARE_H
#include <string>
using std::string;
class Hardware
{
public:
Hardware (int = 0, string = "", int = 0, double = 0);
void setAccountNumber(int);
int getAccountNumber() const;
void setName(string);
string getName() const;
void setAmount(int);
int getAmount() const;
void setPrice(double);
double getPrice() const;
private:
int accountNumber;
string name;
int amount;
double price;
};
#endif
硬件.cpp
#include <string>
using std::string;
#include "hardware.h"
Hardware::Hardware(int accountNumberValue, string nameValue, int amountValue, double priceValue)
{
setAccountNumber(accountNumberValue);
setName(nameValue);
setAmount(amountValue);
setPrice(priceValue);
}
void Hardware::setAccountNumber(int accountValue)
{
accountNumber = accountValue;
}
int Hardware::getAccountNumber() const
{
return accountNumber;
}
void Hardware::setName(string nameValue)
{
name = nameValue;
}
string Hardware::getName() const
{
return name;
}
void Hardware::setAmount(int amountValue)
{
amount = amountValue;
}
int Hardware::getAmount() const
{
return amount;
}
void Hardware::setPrice(double priceValue)
{
price = priceValue;
}
double Hardware::getPrice() const
{
return price;
}