【发布时间】:2014-03-07 19:33:27
【问题描述】:
我正在尝试用二进制写一个字符串。
这是我的代码:
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,它会输出同样的东西。
我误会了什么?
谢谢。
【问题讨论】:
-
你误解了二进制文件模式的意思。请参阅:Difference between opening a file in binary vs text。此外,没有充分的理由在这段代码中使用
new(C++ 不是 Java)。 -
离题了,但是有什么特别的原因你使用指针和
new而不是对象本身? -
@0x499602D2 使用它有什么问题?不想将所有内容都存储在堆栈中。
-
请看this。