views:

342

answers:

3

Dear all,

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;
}
+3  A: 

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

bb
+1  A: 

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.

Wacek
+1  A: 

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.

David Alfonso