本文整理汇总了C++中std::fstream::setstate方法的典型用法代码示例。如果您正苦于以下问题:C++ fstream::setstate方法的具体用法?C++ fstream::setstate怎么用?C++ fstream::setstate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类std::fstream
的用法示例。
在下文中一共展示了fstream::setstate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: OpenUtf8FStreamForRead
void OpenUtf8FStreamForRead(std::fstream &aFStream, const char *aUtf8FileName)
{
// Unfortunately the windows C++ STL library will not open files specified via a UTF-8 filename.
// The workaround is to convert the UTF-8 name to the short DOS compatible name and open that instead
// First convert the UTF-8 name to UTF-16 for use with the windows APIs
WCHAR *utf16Name = WindowsUtf8ToUtf16(aUtf8FileName);
// Obtain short DOS compatible name for file
WCHAR *utf16NameDos = WindowsRetrieveDosName(utf16Name);
if(utf16NameDos == 0)
{
aFStream.setstate(std::ios_base::badbit);
delete [] utf16Name;
return;
}
char *utf8NameDos = WindowsUtf16ToUtf8(utf16NameDos);
aFStream.open(utf8NameDos, std::ios_base::binary | std::ios_base::in);
delete [] utf8NameDos;
delete [] utf16NameDos;
delete [] utf16Name;
return;
}
示例2: OpenUtf8FStreamForWrite
void OpenUtf8FStreamForWrite(std::fstream &aFStream, const char *aUtf8FileName)
{
// Unfortunately the windows C++ STL library will not open files specified via a UTF-8 filename.
// The workaround is to make sure the file already exists and then convert the UTF-8 name to the short DOS compatible name
// and open that instead
// First convert the UTF-8 name to UTF-16 for use with the windows APIs
WCHAR *utf16Name = WindowsUtf8ToUtf16(aUtf8FileName);
// Make sure the file exists
HANDLE fh = CreateFileW((WCHAR *)utf16Name, GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0 );
if(fh == INVALID_HANDLE_VALUE)
{
dbg << Log::Indent() << "Failed to create file '" << aUtf8FileName << "'" << Log::Endl();
aFStream.setstate(std::ios_base::badbit);
delete [] utf16Name;
return;
}
// Close handle
CloseHandle(fh);
// Obtain short DOS compatible name for file
WCHAR *utf16NameDos = WindowsRetrieveDosName(utf16Name);
if(utf16NameDos == 0)
{
aFStream.setstate(std::ios_base::badbit);
delete [] utf16Name;
return;
}
char *utf8NameDos = WindowsUtf16ToUtf8(utf16NameDos);
aFStream.open(utf8NameDos, std::ios_base::binary | std::ios_base::trunc | std::ios_base::out);
delete [] utf8NameDos;
delete [] utf16NameDos;
delete [] utf16Name;
return;
}