无法读取使用Unicode的文件(存在)

Cannot read to file(exist) with UNICODE

本文关键字:文件 存在 Unicode 读取      更新时间:2023-10-16

我有一个项目,需要读取sysdata file的路径。我想移动包含"ç",",","路径的sysdata文件,但无法读取此char char我必须用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");

,但不再起作用。你能帮我吗?

在此处输入图像描述

这种情况是我的问题:(

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

fopen功能打开filename指定的文件。经过 默认,使用ANSI解释狭窄的文件名字符串 codepage(CP_ACP)。

来源。

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

使用C Fstreams用于文件I/O。

时,类似的限制适用

因此,您解决此问题的唯一方法是将您的UTF-8编码路径名转换为系统编码或宽字符串。

fopen通常读取Unicode Chars。尝试更改编码

的文件