问题
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
.
回答1:
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);
来源:https://stackoverflow.com/questions/23936105/fgetws-fails-to-get-the-exact-wide-char-string-from-file