我有一个带有读取二进制文件的problam在c++

发布于 2025-01-30 09:33:26 字数 1605 浏览 4 评论 0原文

我在阅读二进制文件的输入文件时遇到问题,当我编译此代码时,我在阅读SQL文件时会在终端中获得问号。

我的问题是我想将字节转换为char,我尝试使用此代码,但没有结果。

Item::Item(const std::string &file_name) {
    this->stfile.open(file_name,
                      std::ios::in | std::ios::out | std::ios::binary);
    this->input_file.open(file_name,
                          std::ios::in | std::ios::out | std::ios::binary);
    if (this->stfile.is_open() == false) {
        this->stfile.open(file_name, std::ios::in | std::ios::out |
                                         std::ios::binary | std::ios::trunc);
    }
    if (this->stfile.is_open() == false) {
        throw exception::CanNotOpenStorageFile(file_name);
    }
    this->stfile.seekg(0, std::ios::end);

    assert(this->stfile.tellg() % 4096 == 0);
    this->_count_pages = this->stfile.tellg() / 4096;
}

Item::~Item() { this->stfile.close(); }

void Item::read([[maybe_unused]] const Page::id_t page_id,
                [[maybe_unused]] std::byte *buffer) {
    this->stfile.seekg(page_id * 4096, std::ios::beg);

    this->stfile.read(reinterpret_cast<char *>(buffer), 4096);
    for(int i=0; i<4096; i++({
        this->input_file.write(reinterpret_cast<char *>(buffer[i]), 4096);
    }
    this->input_file.close();
}

void Item::write([[maybe_unused]] const Page::id_t page_id,
                 [[maybe_unused]] const std::byte *data) {
    this->stfile.seekp(page_id * 4096, std::ios::beg);
    this->stfile.write(reinterpret_cast<const char *>(data), 4096);
}

i have a problem reading the input file which is binary , when i compile this code i get question marks in the terminal while reading a sql file .

my problem is that i want to convert byte to char , i tried with this code but no result .

Item::Item(const std::string &file_name) {
    this->stfile.open(file_name,
                      std::ios::in | std::ios::out | std::ios::binary);
    this->input_file.open(file_name,
                          std::ios::in | std::ios::out | std::ios::binary);
    if (this->stfile.is_open() == false) {
        this->stfile.open(file_name, std::ios::in | std::ios::out |
                                         std::ios::binary | std::ios::trunc);
    }
    if (this->stfile.is_open() == false) {
        throw exception::CanNotOpenStorageFile(file_name);
    }
    this->stfile.seekg(0, std::ios::end);

    assert(this->stfile.tellg() % 4096 == 0);
    this->_count_pages = this->stfile.tellg() / 4096;
}

Item::~Item() { this->stfile.close(); }

void Item::read([[maybe_unused]] const Page::id_t page_id,
                [[maybe_unused]] std::byte *buffer) {
    this->stfile.seekg(page_id * 4096, std::ios::beg);

    this->stfile.read(reinterpret_cast<char *>(buffer), 4096);
    for(int i=0; i<4096; i++({
        this->input_file.write(reinterpret_cast<char *>(buffer[i]), 4096);
    }
    this->input_file.close();
}

void Item::write([[maybe_unused]] const Page::id_t page_id,
                 [[maybe_unused]] const std::byte *data) {
    this->stfile.seekp(page_id * 4096, std::ios::beg);
    this->stfile.write(reinterpret_cast<const char *>(data), 4096);
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文