wcstombs使用需要设置中文环境,具体修改代码如下:
bool Block::Create(const wchar_t* filename)
// PURPOSE: Create a new block file and open it.
// PURPOSE: If file is present, truncate it and then open it.
// PROMISE: Return true if file is successfully created and opened, false if otherwise.
{
// Create new file
size_t filenameLength = 2*wcslen(filename)+1;
char* name = new char[filenameLength];
size_t converted = 0;
wcstombs_s(&converted, name, filenameLength, filename, _TRUNCATE);
//name[filenameLength] = 0;
setlocale(LC_ALL,"");
file_.open(name, ios_base::out | ios_base::trunc);
setlocale(LC_ALL,"C");
file_.close();
file_.clear();
// Open the file
bool ret = this->Open(filename);
delete[] name;
return ret;
}
bool Block::Open(const wchar_t* filename, ios_base::openmode mode)
// PURPOSE: Open an existing block file.
// PROMISE: Return true if file is successfully opened, false if otherwise.
{
// Open existing file for reading or writing or both
size_t filenameLength = wcslen(filename);
filename_.resize(2*filenameLength+1, 0);
setlocale(LC_ALL,"");
wcstombs(&*(filename_.begin()), filename, 2*filenameLength+1);
file_.open(&*(filename_.begin()), mode | ios_base::binary);
setlocale(LC_ALL,"C");
if (!file_.is_open()) return false;
mode_ = mode;
// Calculate filesize
if (mode & ios_base::in)
{
file_.seekg(0, ios_base::end);
fileSize_ = file_.tellg();
}
else if (mode & ios_base::out)
{
file_.seekp(0, ios_base::end);
fileSize_ = file_.tellp();
}
else
{
this->Close();
return false;
}
// Calculate last index + 1
indexEnd_ = fileSize_/blockSize_ + (fileSize_ % blockSize_ ? 1 : 0);
return true;
}