无法读取使用Unicode的文件(存在)
Cannot read to file(exist) with UNICODE
我有一个项目,需要读取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。尝试更改编码
相关文章:
- 文件存在时的最新检查声明生成输入"is missing"
- GetPrivateProfileStringA 返回"The system cannot find the file specified."即使文件存在
- 自动工具: Makefile.am:如果文件存在,则链接
- 等待文件存在并且不被其他人占用
- 如何修复QFile打开错误(未知错误),即使文件存在
- WINAPI:文件存在检查失败
- 无法读取使用Unicode的文件(存在)
- C++检查文件存在而不打开它
- C++ 路径文件存在限制为 260 个字符
- 如何仅当文件存在时才使用追加模式打开文件
- 文件存在,但打开的文件仍始终返回 -1
- 路径文件存在导致链接器错误 2028/2019
- [UnixODBC][驱动程序管理器]无法打开 lib '/usr/lib/libmyodbc.so' :找不到文件,但文件存在
- 当文件存在时,Linux中的fopen(Lubuntu)会返回NULL
- 无法打开包含文件,尽管前面提到的文件存在
- std::ios::openmode的组合,如果文件存在,则截断,但防止创建新文件
- 当文件存在时,创建文件失败,错误码为2
- 没有这样的文件或目录,只有文件存在
- .cpp文件存在Xcode头文件问题
- 警告 LNK4099:如果文件存在,也找不到 PDB 'vc100.pdb'