我正在制作的这部分程序应该打印出随机存取二进制文件中存储的所有记录。每次运行它时,它都会打印出所有记录,然后抛出访问冲突异常:
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中的类。
hardware.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
hardware.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;
}
答案 0 :(得分:1)
正如约阿希姆所说,你不能像这样读std::string
。
它不是值类型;在内部它将保存一个指向包含其文本的内存区域的指针。如果你从一个文件中读取一个随机的二进制blob,那个字符串指针可能指向任何地方;解除引用它(例如,通过获取或设置'name'的值)可能会引发访问冲突或段错误等等。
我会添加一些新的方法,比如这样(不保证编译或工作!):</ p>
void Hardware::Serialise(std::ostream& stream) const
{
stream << accountName << "\n" << accountNumber << "\n" << accountValue << std::endl;
}
void Hardware::Deserialise(std::istream& stream)
{
char[1024] buffer;
stream.readsome(buffer, 1024);
accountName = buffer;
buffer >> accountNumber >> accountValue;
}
了解对象的内部结构对于(正确)将其正确序列化非常关键。处理失败条件,记录间分隔符以及使用字符串流而不是fxied-size缓冲区来读取名称留给读者。