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:
- 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;
}