fgetws无法从FILE获取确切的宽字符串* [英] fgetws fails to get the exact wide char string from FILE*

查看:161
本文介绍了fgetws无法从FILE获取确切的宽字符串*的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用fgetws从FILE中逐行获取一些字符串。我拥有的文件来自popen命令。这里是代码片段:

  FILE * pInstalledApps = popen(command.c_str(),r); 
if(NULL!= pInstalledApps)
{
wchar_t currentAppPath [kMaximumAppPathLength];

//一次读取一行应用程序路径。
while(!feof(pInstalledApps))
{
if(fgetws(currentAppPath,kMaximumAppPathLength,pInstalledApps)== NULL)
{
break;
}
wchar_t * pCharPos = NULL;
if((pCharPos = wcschr(currentAppPath,L'\ n'))!= NULL)
{
* pCharPos = L'\ 0;
}
std :: wstring appPath(currentAppPath);

//用wstring
}
做一些事情pclose(pInstalledApps);





$ b当我得到的字符串currentAppPath有宽字符串时,得不到预期的字符串。例如,如果我从FILE获得的字符串是10teciêênks,那么我的appPath变量将会有10tecieÌeÌnks

解决方案

这看起来像一个编码(或更具体的解码)问题。宽字符API函数不会自动检测数据的字符编码。例如:

  #include< locale.h> 

setlocale(LC_ALL,en.UTF-8);

fgetws 的手册页显示:

  fgetws()的行为取决于当前语言环境的LC_CTYPE类别。 

所以使用:

  setlocale(LC_CTYPE,en.UTF-8); 

也应该有效。

上面假设数据是UTF-8编码的。

UPDATE:可以通过执行如下操作来保留当前的语言环境:

  char * prev_locale = strdup(setlocale(LC_CTYPE,NULL)); 
setlocale(LC_CTYPE,en.UTF-8);
// ...
setlocale(LC_CTYPE,prev_locale);
free(prev_locale);


I am using fgetws to get some string line by line from a FILE. The FILE I have is from a popen command. Here is the code snippet:

    FILE* pInstalledApps = popen( command.c_str(), "r" );
    if( NULL != pInstalledApps )
    {
        wchar_t currentAppPath [kMaximumAppPathLength];

        // Reading app paths one line at a time.
        while ( ! feof (pInstalledApps) )
        {
            if ( fgetws  ( currentAppPath, kMaximumAppPathLength, pInstalledApps) == NULL )
            {
                break;
            }
            wchar_t *pCharPos = NULL;
            if ( ( pCharPos = wcschr( currentAppPath, L'\n' ) ) != NULL )
            {
                *pCharPos = L'\0';
            }
            std::wstring appPath( currentAppPath );

                            //Do something with the wstring
        }
        pclose( pInstalledApps );
    }

When the string currentAppPath that i gets has wide char strings, the appPath that I get doesnot have the expected string. For example if the string that I get from the FILE is 10teciêênks my appPath variable will be having 10tecieÌeÌnks.

解决方案

That looks like an encoding (or more specifically, decoding) issue. The wide-character API functions will not automatically detect the character encoding of the data. You need to set this in the application, for example:

#include <locale.h>

setlocale(LC_ALL, "en.UTF-8");

The man page for fgetws states:

The behavior of fgetws() depends on the LC_CTYPE category of the current locale.

so using:

setlocale(LC_CTYPE, "en.UTF-8");

should also work.

NOTE: The above assumes the data is UTF-8 encoded.

UPDATE: It is possible to preserve the current locale by doing something like:

char *prev_locale = strdup(setlocale(LC_CTYPE, NULL));
setlocale(LC_CTYPE, "en.UTF-8");
// ...
setlocale(LC_CTYPE, prev_locale);
free(prev_locale);

这篇关于fgetws无法从FILE获取确切的宽字符串*的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆