std::ofstream由二进制流写文件的问题
从MPQ包中读取二进制流出来然后文件写到硬盘。
DWORD size = SFileGetSize(hFile);
char* buffer = new char[size];
std::ofstream ofs;
ofs.open(name);
ASSERT(ofs.good());
ofs.write(buffer, size);
ASSERT(ofs.good());
ofs.close();
.xml,.lua,等文本都是对的,唯独.jpg这些图片些不对!
open改为ofs.open(name, std::ios::out | std::ios::binary);
就对了!
原因应该是ofstream打开文件时默认是文本格式吧。。。
void open( const char *_Filename, ios_base::openmode _Mode = ios_base::out, int _Prot = (int)ios_base::_Openprot ); void open( const char *_Filename, ios_base::openmode _Mode ); void open( const wchar_t *_Filename, ios_base::openmode _Mode = ios_base::out, int _Prot = (int)ios_base::_Openprot ); void open( const wchar_t *_Filename, ios_base::openmode _Mode );