在 c++ 中的溢出链接中使用 fstream 文件

发布于 2024-07-24 12:43:43 字数 2020 浏览 7 评论 0原文

我有一个文件,我想使用记录读取该文件并将其写入二进制文件。 一开始我有一个空文件,我想添加新记录,但是当我使用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++;
        }
    }

如果有人能告诉我为什么我不能向文件写入任何内容,我将非常感激。

提前致谢,

格雷格

I have a file that I want to read and write to a binary file using records. In the beginning I have an empty file and I want to add new record, but when I use the seekp function, then the location is at (-1) is it ok? Because when I check, I see that it hasnt written anything to the file. See code:

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++;
        }
    }

If anyone can tell me why I cant write anything to the file I will really appriciate it.

Thanks in advance,

Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-07-31 12:43:44

显然您正在阅读文件末尾。 您的 seekg 调用可能会将读取指针移过末尾,然后调用 read。 这会将您的流设置为失败状态。 通过执行以下操作来检查这一点:

std::cout << dataFile.fail() << std::endl;

read 调用之后。

您尝试进行的记录管理并不是一项简单的任务。 也许您应该尝试使用无服务器 DBE,例如 SQLite 或其他序列化库,例如 Boost.Serialization

另外,您还应该使用 ios::binary 标志打开文件。

You are apparently reading past the end of the file. Your seekg call is probably moving the read pointer past the end, then you call read. This will set your stream into a fail state. Check this by doing:

std::cout << dataFile.fail() << std::endl;

after the read call.

The kind of record management you are trying to do isn't a trivial task. Maybe you should try a serverless DBE like SQLite or other serialization libraries like Boost.Serialization.

Also, you should open your file with the ios::binary flag as well.

临风闻羌笛 2024-07-31 12:43:43

这里有一些可能有帮助的建议:

  • 当您打开文件时,使用 ios_base::binary 标志
  • 确保 Book 是 POD(即 C 兼容类型)
  • 确保当您读取或写入时流位于之前和之后的有效状态
  • 不要使用 readBook.getId() == -1 来检查读取是否成功
  • 确保当您查找流时您不会超过文件末尾
  • 使用缓冲区来获取总数文件中的字节数,然后确保在
  • 每次查找之前都不会超过它,进行相对查找(例如使用 ios_base::beg)
  • 使用 static_cast(static_cast(&book))reinterpret cast

如果您对这些建议有任何具体问题,请告诉我们,也许我们可以更好地指导您。

Well here are a few suggestions that may help:

  • when you open the file, use ios_base::binary flag
  • make sure that Book is a POD (i.e. a C compatible type)
  • make sure that when you read or write that the stream is in a valid state before and after
  • don't use readBook.getId() == -1 to check if the read succeeded
  • make sure that when you seek into the stream you're not going past the end of file
  • use the buffer to get the total number of bytes in the file and then ensure that you don't exceed it before seeking
  • whenever you seek, do a relative seek (use ios_base::beg for e.g)
  • use static_cast<char*>(static_cast<void*>(&book)) vs reinterpret cast<char*>

If you have any specific questions about any of those suggestions, let us know and perhaps we can guide you better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文