C++ 二进制文件:ios::二进制文件不工作

C++ 二进制文件:ios::二进制文件不工作,c++,binary,C++,Binary,我想用二进制写一个字符串 这是我的密码: bool Database::createTable(string name, string content) { string *filePath = new string(*this->path + "/" + name + ".dt"); ifstream *checkFile = new ifstream(*filePath); if(checkFile->good()) { ret

我想用二进制写一个字符串

这是我的密码:

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;
}
我希望在文本编辑器中看到类似的内容:

6a6f 686e 0000 0000 0000 0000 1500 0000
1039 4099 b67f 0000 1c39 4099 b67f 0000
但我看到的是纯文本

如果我取出
ios::binary
,它将输出相同的内容

我误解了什么


谢谢。

要获得这样的格式,请使用hexdump工具。从C++开始:


您误解了二进制文件模式的含义。请参阅:。此外,在这段代码中没有很好的理由使用
new
(C++不是Java)。离题了,但是有什么特别的原因让你使用指针和
new
,而不是对象本身吗?@0x499602D2使用它有什么问题?不希望在堆栈上存储所有内容。请参阅。