c++ - 无法使用 UNICODE 读取文件(存在)

标签 c++ visual-studio-2010 unicode utf-8 fread

我有一个项目需要读取 SysData 文件的路径。我想移动包含“ç”、“ş”、“ğ”路径的 SysData 文件,但无法读取此字符。我必须使用 UNICODE(就像那个 utf-8)。

有代码;

bool TSimTextFileStream::ReadLine  ( mstring * str )
{
        *str = "";
        char c = ' ';
        bool first = true;
        // while ( read ( hFile, &c, 1 ) )
        while ( fread ( &c, 1, 1, hFile ) )
        {
                if (first) first = false;
                #ifdef __linux__
                        if ( c == 13 )
                                continue;
                                else
                        if ( c == 10 )
                                break;
                                else
                                *str += c;
                #else
                         if( c == 13 || c == 10)
                             break;
                         else
                             *str += c;

                #endif
        }
        return !first;
}

并且有代码,调用这个方法;

mstring GetSysDataDirectory ( )
{
    static mstring sysDataDir = "";
    if ( sysDataDir == "" )
    {
    if (mIsEnvironmentVarExist("SYSDATAPATH"))
    {
      mstring folder = mGetEnvVar("SYSDATAPATH");

      if (folder.size() == 0)
      {
        folder = mGetCurrentDir ( ) + "/SysData";
      }

      sysDataDir = folder;
    }
        else if ( mIsFileExist ( "SysDataPath.dat" ) )
        {
            TSimTextFileStream txtfile;
            txtfile.OpenFileForRead( "SysDataPath.dat" );
            mstring folder;
            if ( txtfile.ReadLine( &folder ) )
            {
                sysDataDir = folder;
            }
            else
            {
                sysDataDir = mGetCurrentDir ( ) + "/SysData";
            }
        }
        else
        {
            sysDataDir = mGetCurrentDir ( ) + "/SysData";
        }
    }

    return sysDataDir;
}

我搜索并找到了一些解决方案,但没有像那样工作;

bool TSimTextFileStream::OpenFileForRead(mstring fname)
{
        if (hFile != NULL) CloseFile();

        hFile = fopen(fname.c_str(), "r,ccs=UNICODE");

        if (hFile == NULL) return false; else return true;
}

并尝试了这个;

hFile = fopen(fname.c_str(), "r,ccs=UTF-8");

但不能再工作了。你能帮帮我吗?

enter image description here

这种情况是我的问题:((

最佳答案

Windows 不支持 fopen 的 UTF-8 编码路径名:

The fopen function opens the file that is specified by filename. By default, a narrow filename string is interpreted using the ANSI codepage (CP_ACP).

Source .

相反,提供了第二个函数,称为 _wfopen,它接受一个宽字符字符串作为路径参数。

类似的限制适用 when using the C++ fstreams用于文件 I/O。

因此解决此问题的唯一方法是将 UTF-8 编码的路径名转换为系统代码页或 to a wide character string .

关于c++ - 无法使用 UNICODE 读取文件(存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46153808/

相关文章:

c# - 如何在 Visual C++ 中使用使用 C# 创建的 dll?

c# - 尝试将 clickonce 应用程序部署到 FTP 站点但未安装 FrontPage 服务器扩展错误

c++ - g++ unicode 字符 ifstream

c++ - 等待 DBus 服务在 Qt 中可用

用于集群和 HPC 的 C++ 编程

C++ - 无法从 'char []' 转换为 'char[]'

c# - Visual Studio 错误?

C++ win32 GUI编程,最短路径?

javascript - 如何从 JavaScript 中的范围中获取随机字符?

c++ - en_US.UTF-8 区域设置的 Windows 等效项是什么?