多个文件合二为一
我正在尝试创建我自己的文件格式。我想存储图像文件 以及该文件中的一些文本描述。 文件格式将类似于:
image_file_size
image_data
desctiption_file_size
description_data
但没有“\n”符号。 为此,我使用 std::ios::binary。这是一些代码, 描述该过程(这是草图,不是最后一个变体): 写我的文件。
long long image_length, desctiption_length;
std::fstream m_out(output_file_path, std::ios::out |
std::ios::binary);
std::ifstream input_image(m_image_file_path.toUtf8().data());
input_image.seekg(0, std::ios::end);
image_length = input_image.tellg();
input_image.seekg(0, std::ios::beg);
// writing image length to output file
m_out.write( (const char *)&image_length, sizeof(long long) );
char *buffer = new char[image_length];
input.read(buffer, image_length);
// writing image to file
m_out.write(buffer, image_length);
// writing description file the same way
// ...
正在阅读我的文件。
std::fstream m_in(m_file_path.toUtf8().data(), std::ios::in );
long long xml_length, image_length;
m_in.seekg(0, std::ios::beg);
m_in.read((char *)&image_length, sizeof(long long));
m_in.seekg(sizeof(long long));
char *buffer = new char[image_length];
m_in.read(buffer, image_length );
std::fstream fs("E:\\Temp\\out.jpg");
fs.write(buffer, image_length);
现在图像(E:\Temp\out.jpg)已损坏。我正在看十六进制 编辑器,还有一些额外的位。
有人可以帮助我并告诉我我做错了什么吗?
I'm trying to create my own file format. I want to store image file
and some text description in that file.
File format will be something like that:
image_file_size
image_data
desctiption_file_size
description_data
But without '\n' symbols.
For that purpose i'm using std::ios::binary. Here is some code,
describes that process (it is sketch, not last variant):
Writing my file.
long long image_length, desctiption_length;
std::fstream m_out(output_file_path, std::ios::out |
std::ios::binary);
std::ifstream input_image(m_image_file_path.toUtf8().data());
input_image.seekg(0, std::ios::end);
image_length = input_image.tellg();
input_image.seekg(0, std::ios::beg);
// writing image length to output file
m_out.write( (const char *)&image_length, sizeof(long long) );
char *buffer = new char[image_length];
input.read(buffer, image_length);
// writing image to file
m_out.write(buffer, image_length);
// writing description file the same way
// ...
Reading my file.
std::fstream m_in(m_file_path.toUtf8().data(), std::ios::in );
long long xml_length, image_length;
m_in.seekg(0, std::ios::beg);
m_in.read((char *)&image_length, sizeof(long long));
m_in.seekg(sizeof(long long));
char *buffer = new char[image_length];
m_in.read(buffer, image_length );
std::fstream fs("E:\\Temp\\out.jpg");
fs.write(buffer, image_length);
And now image (E:\Temp\out.jpg) is broken. I'm watching it in hex
editor and there are some extra bits.
Can somebody help me and tell what i'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您在任何地方都存储和读取二进制数据,因此您应该以二进制模式打开和创建所有文件。
在写入部分:
在读取部分:
Since you are storing and reading binary data everywhere, you should open and create all files in binary mode.
In the write part:
In the read part: