我有一个项目需要读取 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");
但不能再工作了。你能帮帮我吗?
这种情况是我的问题:((
最佳答案
Windows 不支持 fopen
的 UTF-8 编码路径名:
The
fopen
function opens the file that is specified byfilename
. 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/