标签: wide-api

使用非MSVC编译器在Windows下打开带有Unicode文件名的文件的fstream

当文件名是"Unicode"文件名时,我需要打开一个文件作为std :: fstream(或实际上任何其他std :: ostream).

在MSVC下我有非标准的扩展名std::fstream::open(wchar_t const *,...)?我可以用其他编译器做什么,比如GCC(最重要的)和Borland编译器.

我知道CRTL提供_wfopen但是它提供了C FILE *接口而不是io-stream,也许有一种非标准的方法来创建io-stream FILE *?有没有boost::ifstream像MSVC一样的Windows扩展?

c++ windows unicode gcc wide-api

10
推荐指数
2
解决办法
9345
查看次数

utfcpp和Win32广泛的API

是否好/安全/可以使用微小的utfcpp库将我从宽Windows API(FindFirstFileW等)返回的所有内容转换为使用utf16to8的有效UTF8表示?

我想在内部使用UTF8,但是无法获得正确的输出(在另一次转换或普通cout之后通过wcout).普通的ASCII字符当然可以工作,但是ñä搞砸了.

还是有更简单的选择?

谢谢!

更新:感谢Hans(下面),我现在可以通过Windows API进行简单的UTF8 < - > UTF16转换.双向转换有效,但UTF16字符串中的UTF8有一些额外的字符可能会在以后引起我的麻烦......).我将在这里分享它纯粹的友好:)):

// UTF16 -> UTF8 conversion
std::string toUTF8( const std::wstring &input )
{
    // get length
    int length = WideCharToMultiByte( CP_UTF8, NULL,
                                      input.c_str(), input.size(),
                                      NULL, 0,
                                      NULL, NULL );
    if( !(length > 0) )
        return std::string();
    else
    {
        std::string result;
        result.resize( length );

        if( WideCharToMultiByte( CP_UTF8, NULL,
                                 input.c_str(), input.size(),
                                 &result[0], result.size(),
                                 NULL, NULL ) > 0 )
            return result;
        else
            throw std::runtime_error( "Failure to execute toUTF8: conversion failed." …
Run Code Online (Sandbox Code Playgroud)

c++ winapi utf-8 utf-16 wide-api

3
推荐指数
1
解决办法
2359
查看次数

标签 统计

c++ ×2

wide-api ×2

gcc ×1

unicode ×1

utf-16 ×1

utf-8 ×1

winapi ×1

windows ×1