0

I'm trying to write a string in binary.

Here is my code:

bool Database::createTable(string name, string content)
{
    string *filePath = new string(*this->path + "/" + name + ".dt");

    ifstream *checkFile = new ifstream(*filePath);

    if(checkFile->good())
    {
        return false;
    }

    ofstream *file = new ofstream(*filePath, ios::binary);

    file->write(content.c_str(), content.size());

    file->close();

    delete filePath;
    delete checkFile;
    delete file;

    return true;
}

I would like to see something like that in a text editor:

6a6f 686e 0000 0000 0000 0000 1500 0000
1039 4099 b67f 0000 1c39 4099 b67f 0000

But I'm seeing plain text.

If I take out the ios::binary, it outputs the same thing.

What am I misunderstanding?

Thanks.

Maurício Giordano
  • 2,597
  • 3
  • 26
  • 54
  • You misunderstand the meaning of _binary file mode_. See: [Difference between opening a file in binary vs text](http://stackoverflow.com/questions/20863959/difference-between-opening-a-file-in-binary-vs-text). In addition, there's no good reason to use `new` in this code (C++ is not Java). – Blastfurnace Mar 07 '14 at 19:45
  • 1
    Off topic but is there any particular reason you're using pointers and `new` instead of the objects themselves? – 0x499602D2 Mar 07 '14 at 19:46
  • @0x499602D2 what's the problem using it? Don't want to store everything on stack. – Maurício Giordano Mar 07 '14 at 22:52
  • 1
    Please see [this](http://stackoverflow.com/questions/6500313/c-why-should-new-be-used-as-little-as-possible). – 0x499602D2 Mar 07 '14 at 23:04

1 Answers1

0

In order to get such a format, use an hexdump tool. From C++:

How would I create a hex dump utility in C++?

Community
  • 1
  • 1
Iban Cereijo
  • 1,520
  • 1
  • 14
  • 25