使用输出流创建的二进制输出文件的内容
该代码编译并执行。 它只是打印内容 转换为二进制格式。 然而,输出与我的预期不同,即:
- 输出文件大小应该比使用 std::cout 创建的文件小得多。
- 输出文件的内容应该被压缩,因此当我们在编辑器中打开它时, 我们应该看不到内容。
但为什么下面的代码没有达到我希望的效果呢? 我怎样才能相应地修改它?
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void WriteStr2BinFh(const string& St, ostream &fn)
{
fn.write(St.c_str(), St.length());
}
int main ( int arg_count, char *arg_vec[] ) {
vector <string> Tags;
// In principle we have millions of such tags
// Hence we need to compress it into binary output.
Tags.push_back("0000000000");
Tags.push_back("0000101110");
Tags.push_back("0133030122");
Tags.push_back("0133132033");
Tags.push_back("1002013320");
Tags.push_back("1111111111");
Tags.push_back("1301013133");
Tags.push_back("3010112111");
Tags.push_back("3203012113");
Tags.push_back("3203012212");
//prn_vec<string>(Tags, "\t");
//cout << endl;
ofstream outfile;
outfile.open("MyOut.bin", ios::binary|ios::out);
for (unsigned i=0; i <Tags.size(); i++) {
WriteStr2BinFh(Tags[i]+"\t",outfile);
}
outfile.close();
return 0;
}
This code compiles and does execute. It simply print the content
into a binary format. However the output differs from what I expected, namely:
- Output file size should be much smaller that those created with std::cout.
- The content of output file should be compressed, hence when we open it in editor,
we should not be able to see the content.
But why the code below doesn't do as I hope it does? How can I modify it accordingly?
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
void WriteStr2BinFh(const string& St, ostream &fn)
{
fn.write(St.c_str(), St.length());
}
int main ( int arg_count, char *arg_vec[] ) {
vector <string> Tags;
// In principle we have millions of such tags
// Hence we need to compress it into binary output.
Tags.push_back("0000000000");
Tags.push_back("0000101110");
Tags.push_back("0133030122");
Tags.push_back("0133132033");
Tags.push_back("1002013320");
Tags.push_back("1111111111");
Tags.push_back("1301013133");
Tags.push_back("3010112111");
Tags.push_back("3203012113");
Tags.push_back("3203012212");
//prn_vec<string>(Tags, "\t");
//cout << endl;
ofstream outfile;
outfile.open("MyOut.bin", ios::binary|ios::out);
for (unsigned i=0; i <Tags.size(); i++) {
WriteStr2BinFh(Tags[i]+"\t",outfile);
}
outfile.close();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的意思是“用 std::cout 创建”?
如果您要保存整数而不是字符串,它可能会小一些。
不,它不应该被压缩。 您可以使用Boost.Iostreams库 http://www .boost.org/doc/libs/1_38_0/libs/iostreams/doc/index.html 用于创建压缩文件。
为了便于理解,您可以认为二进制文件包含您在查找内存时可以在调试器中看到的信息。
另外,对于以二进制格式输出,您应该对所有向量项使用写入流方法(如果使用
std::vector
,则会有所不同)。 (对于输出 \t 您可以使用运算符 << )What you mean "created with std::cout"?
It could be a little smaller if you will save ints, not strings.
No, it shouldn't be compressed. You could use Boost.Iostreams library http://www.boost.org/doc/libs/1_38_0/libs/iostreams/doc/index.html for create zipped files.
For easy understanding you could think that binary file contain information which you could see in debugger when will looking memory.
Also for outputting in binnary format you should use write stream method for all vector items (in case with
std::vector < int >
it will have difference). ( for output \t you could use operator << )您必须以二进制格式(而不是文本)写入数据:
您必须知道像 long 这样的类型有一些最大值,因此您可能必须将字符串拆分为 n 段并保存为 n long。
You must write data in binary format (not text):
You must be aware that types like long have some maximum values, so you will probably have to split your string into n pieces and save as n longs.
恐怕 IOStream 库不会对您的输出应用任何压缩。 正如 bb 指出的,您应该使用另一个库来压缩您的流。
由于前面的参数,被视为字节流(恰好是 ASCII 表示的字符)的输出被“按原样”写入文件,因此大小不会改变。
查看一些文档和一般二进制文件的更好解释。
I'm afraid that the IOStream Library doesn't apply any compression to your output. As bb pointed out, you should use another library to get your stream compressed.
As a consequence of the previous argument, the output, which is treated as a stream of bytes (which happen to be ASCII represented characters) is written "as is" to the file, and, therefore, the size will not change.
Take a look at some documentation and a better explanation of binary files in general.