使用输出流创建的二进制输出文件的内容

发布于 2024-07-15 06:28:03 字数 1298 浏览 4 评论 0原文

该代码编译并执行。 它只是打印内容 转换为二进制格式。 然而,输出与我的预期不同,即:

  1. 输出文件大小应该比使用 std::cout 创建的文件小得多。
  2. 输出文件的内容应该被压缩,因此当我们在编辑器中打开它时, 我们应该看不到内容。

但为什么下面的代码没有达到我希望的效果呢? 我怎样才能相应地修改它?

#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:

  1. Output file size should be much smaller that those created with std::cout.
  2. 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 技术交流群。

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

发布评论

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

评论(3

终陌 2024-07-22 06:28:03

输出文件大小应该很大
比创建的小
std::cout

你的意思是“用 std::cout 创建”?
如果您要保存整数而不是字符串,它可能会小一些。

输出文件的内容应该是
压缩的,因此当我们打开它时
编辑器,我们应该看不到
内容。

不,它不应该被压缩。 您可以使用Boost.Iostreams库 http://www .boost.org/doc/libs/1_38_0/libs/iostreams/doc/index.html 用于创建压缩文件。

为了便于理解,您可以认为二进制文件包含您在查找内存时可以在调试器中看到的信息。

另外,对于以二进制格式输出,您应该对所有向量项使用写入流方法(如果使用 std::vector ,则会有所不同)。 (对于输出 \t 您可以使用运算符 << )

Output file size should be much
smaller that those created with
std::cout

What you mean "created with std::cout"?
It could be a little smaller if you will save ints, not strings.

The content of output file should be
compressed, hence when we open it in
editor, we should not be able to see
the content.

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 << )

安稳善良 2024-07-22 06:28:03

您必须以二进制格式(而不是文本)写入数据:

void WriteStr2BinFh(const string& St, ostream &fn)
{
char *p = 0;
long l = strtol(St.c_str(), &p);
fn << l;
}

您必须知道像 long 这样的类型有一些最大值,因此您可能必须将字符串拆分为 n 段并保存为 n long。

You must write data in binary format (not text):

void WriteStr2BinFh(const string& St, ostream &fn)
{
char *p = 0;
long l = strtol(St.c_str(), &p);
fn << l;
}

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.

几味少女 2024-07-22 06:28:03

输出文件的内容应该被压缩,因此当我们在编辑器中打开它时,我们不应该看到内容。

恐怕 IOStream 库不会对您的输出应用任何压缩。 正如 bb 指出的,您应该使用另一个库来压缩您的流。

输出文件大小应该比使用 std::cout 创建的文件小得多。

由于前面的参数,被视为字节流(恰好是 ASCII 表示的字符)的输出被“按原样”写入文件,因此大小不会改变。

查看一些文档一般二进制文件的更好解释

The content of output file should be compressed, hence when we open it in editor, we should not be able to see the content.

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.

Output file size should be much smaller that those created with std::cout.

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.

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