我有一个文件,我想使用记录读取和写入二进制文件。一开始我有一个空文件,我想添加新记录,但是当我使用seekp函数时,位置在(-1)是否可以?因为当我检查时,我发现它没有写任何文件。见代码:
void Library::addBook(Book newBook)
{
fstream dataFile;
dataFile.open("BookData.dat", ios::in | ios::out);
if (!dataFile)
{
cerr << "File could not be opened" << endl;
}
int hashResult = newBook.getId() % 4 + 1; // The result of the hash function
// Find the right place to place the new book
dataFile.seekg((hashResult - 1) * sizeof(Book), ios::beg);
Book readBook;
dataFile.read(reinterpret_cast<char*>(&readBook), sizeof(Book));
// The record doesnt exist or it has been deleted
if (readBook.getId() == -1)
{
// The record doesnt exist
if (readBook.getIdPtr() == -1)
{
dataFile.seekp((hashResult - 1) * sizeof(Book));
dataFile.write(reinterpret_cast<char*>(&newBook), sizeof(Book));
}
// The record has been deleted or there is already such record with such hash function
// so we need to follow the pointer to the overflow file
else
{
newBook.setIsBookInData(false); // New book is in overflow file
overflowFile.seekg((readBook.getIdPtr() - 1) * sizeof(Book));
overflowFile.read(reinterpret_cast<char*>(&readBook), sizeof(Book));
// Follow the chain
while (readBook.getIdPtr() != -1)
{
overflowFile.seekg((readBook.getIdPtr() - 1) * sizeof(Book));
overflowFile.read(reinterpret_cast<char*>(&readBook), sizeof(Book));
}
readBook.setIdPtr(header); // Make the pointer to point to the new book
overflowFile.seekp((header - 1) * sizeof(Book));
overflowFile.write(reinterpret_cast<char*>(&newBook), sizeof(Book));
header++;
}
}
如果有人能告诉我为什么我不能在文件中写任何东西,我会真的很喜欢它。
提前致谢,
格雷格
答案 0 :(得分:2)
以下是一些可能有用的建议:
static_cast<char*>(static_cast<void*>(&book))
vs reinterpret cast<char*>
如果您对这些建议有任何具体问题,请告诉我们,也许我们可以为您提供更好的指导。
答案 1 :(得分:1)
您显然正在阅读文件的末尾。您的搜索电话可能会将读取指针移到结尾,然后您调用读取。这会将您的流设置为失败状态。通过执行以下操作来检查:
std::cout << dataFile.fail() << std::endl;
在阅读电话后。
您尝试执行的记录管理类型并非易事。也许您应该尝试像SQLite这样的无服务器DBE或其他序列化库,例如Boost.Serialization。
此外,您还应该使用 ios :: binary 标志打开文件。