EDN Admin
Well-known member
<span style="color:#222222; font-familyroid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left (I am using Visual Studio 2008 with VC++)<br style="font-size:14px; font-familyroid Serif,Georgia,Times New Roman,serif; color:#222222; line-height:23px; text-align:left
<span style="color:#222222; font-familyroid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left My MFC application reads a text file and uses its contents to alter the text on the buttons of the GUI. The application
uses MBCS. Recently, I was asked to add support for Russian text. I can use Notepad to create the text file I need (in Unicode), but when I convert the Unicode to MBCS, I get gibberish. Here is a code sample:
<pre class="prettyprint #include <locale.h>
#define SETLOCALE(locale)
if ( !setlocale(LC_ALL, locale) )
{
AfxMessageBox("setlocale failed for " locale ", this locale is probably not installed on your system");
}
CStringList* ReadFile(const CString& strPath)
{
CStringList* pList = new CStringList();
FILE* pFile;
errno_t error = _tfopen_s(&pFile, strPath, "r");
if ( error == 0 )
{
// See if the file is Unicode
char buf[10];
_fgetts(buf, 3, pFile);
if ( (buf[0] == -1) && (buf[1] == -2) )
{
// File is unicode. Have to close and reopen the file, then strip off the BOM
fclose(pFile);
error = _tfopen_s(&pFile, strPath, "rb");
if ( error == 0 )
{
wchar_t buf[1000];
fgetws(buf, 2, pFile);
TCHAR mbsBuf[1000];
SETLOCALE("Russian");
while ( fgetws(buf, 999, pFile) != NULL )
{
size_t dummy;
wcstombs_s(&dummy, mbsBuf, 1000, buf, 1000);
CString strLine(mbsBuf);
strLine.Trim();
AfxMessageBox(strLine);
pList->AddTail(strLine);
}
}
}
fclose(pFile);
}
return pList;
}[/code]
<br/>
<span style="color:#222222; font-familyroid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left Until I added the SETLOCALE("Russian"), the wcstombs() function failed with a EILSEQ error. Now, all I get are characters
that are in the extended ASCII range.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px I tried replacing wcstombs() with WideCharToMultiByte() with no success. I tried several code pages.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Can anyone help?
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Thanks!
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px JAB
<br/>
View the full article
<span style="color:#222222; font-familyroid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left My MFC application reads a text file and uses its contents to alter the text on the buttons of the GUI. The application
uses MBCS. Recently, I was asked to add support for Russian text. I can use Notepad to create the text file I need (in Unicode), but when I convert the Unicode to MBCS, I get gibberish. Here is a code sample:
<pre class="prettyprint #include <locale.h>
#define SETLOCALE(locale)
if ( !setlocale(LC_ALL, locale) )
{
AfxMessageBox("setlocale failed for " locale ", this locale is probably not installed on your system");
}
CStringList* ReadFile(const CString& strPath)
{
CStringList* pList = new CStringList();
FILE* pFile;
errno_t error = _tfopen_s(&pFile, strPath, "r");
if ( error == 0 )
{
// See if the file is Unicode
char buf[10];
_fgetts(buf, 3, pFile);
if ( (buf[0] == -1) && (buf[1] == -2) )
{
// File is unicode. Have to close and reopen the file, then strip off the BOM
fclose(pFile);
error = _tfopen_s(&pFile, strPath, "rb");
if ( error == 0 )
{
wchar_t buf[1000];
fgetws(buf, 2, pFile);
TCHAR mbsBuf[1000];
SETLOCALE("Russian");
while ( fgetws(buf, 999, pFile) != NULL )
{
size_t dummy;
wcstombs_s(&dummy, mbsBuf, 1000, buf, 1000);
CString strLine(mbsBuf);
strLine.Trim();
AfxMessageBox(strLine);
pList->AddTail(strLine);
}
}
}
fclose(pFile);
}
return pList;
}[/code]
<br/>
<span style="color:#222222; font-familyroid Serif,Georgia,Times New Roman,serif; font-size:14px; line-height:23px; text-align:left Until I added the SETLOCALE("Russian"), the wcstombs() function failed with a EILSEQ error. Now, all I get are characters
that are in the extended ASCII range.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px I tried replacing wcstombs() with WideCharToMultiByte() with no success. I tried several code pages.
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Can anyone help?
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px Thanks!
<p style="text-align:left <span style="color:#222222 <span style="font-size:14px; line-height:23px JAB
<br/>
View the full article