我有一个带有读取二进制文件的problam在c++
我在阅读二进制文件的输入文件时遇到问题,当我编译此代码时,我在阅读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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论