Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! My unicode MFC application not able to read ANSI encoded text file.
and also if i change the project settings Unicode to MBCS then it is able to read ANSI text file. but not able to unicode text file.

Please do the needful...

The below is code snippet
C++
FILE *fpRead;
	const int l_size = 1024;
	TCHAR file_buffer[l_size];
	if(	(fpRead = _tfopen(strFilePath, _T("rb")) )== NULL)
	{			
		AfxMessageBox(_T("File not open"));
		return;
	}
	fseek(fpRead, 2, SEEK_SET);
	memset(file_buffer,'\0',sizeof(file_buffer));
	long l_file_pos = ftell(fpRead);		
	CString ss=_T("");		
	while(!feof(fpRead))
	{
		size_t count = fread(file_buffer, l_size+1,2, fpRead);
		AfxMessageBox(file_buffer);	
	}

Thanks and Regards
Sam
Posted
Updated 27-Jan-13 20:12pm
v2

Take a look at my tip Handling simple text files in C/C++[^], for converting a file's content into the form that you want.
 
Share this answer
 
if your file is ANSI then read as ANSI
C++
FILE *fpRead;
	char *file_buffer;
	if(	(fpRead = _tfopen(strFilePath, _T("rb")) )== NULL)
	{			
		AfxMessageBox(_T("File not open"));
		return;
	}
        fseek (fpRead, 0, SEEK_END);   // non-portable
        unsigned long  size=ftell (fpRead);
        file_buffer=new char[size];
        fread(file_buffer, 1, size, file_buffer);
        CString wfile_buffer;
        wfile_buffer=UTF8toUTF16(file_buffer); //definition stated below, apparently this function is not safe
	AfxMessageBox(wfile_buffer);
        delete file_buffer;


CStringW UTF8toUTF16(const CStringA& utf8)
{
   CStringW utf16;
   int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
   if (len>1)
   { 
      wchar_t *ptr = utf16.GetBuffer(len-1);
      if (ptr) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ptr, len);
      utf16.ReleaseBuffer();
   }
   return utf16;
}
 
Share this answer
 
v2
Comments
Mr Sam 28-Jan-13 4:31am    
This function is not working, getting errors on vc6.0. There is not CStringW, CStringA is not there CString only. Please check...

CStringW CFunctions::UTF8toUTF16(const CStringA& utf8)
{
CStringW utf16;
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (len>1)
{
wchar_t *ptr = utf16.GetBuffer(len-1);
if (ptr) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ptr, len);
utf16.ReleaseBuffer();
}
return utf16;
}
Mohibur Rashid 28-Jan-13 4:41am    
did you remove the CFunctions:: before? CStringW is UNICODE version of character string CStringA is the ANSII version.....

it must have to be there. Even most of the Win32 API has two version. as example:
GetWindowLong is defined in header file as

#ifdef UNICODE
#define GetWindowLong GetWindowLongW
#else
#define GetWindowLong GetWindowLongA
#endif // !UNICODE

you can directly use anyone of them....................

But in this case you must have to use specific version of string otherwise you must get invalid result.
Mr Sam 28-Jan-13 4:52am    
I have converted the code like this
CString UTF8toUTF16(const CString& utf8)
{
CString utf16;
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
if (len>1)
{
wchar_t *ptr = utf16.GetBuffer(len-1);
if (ptr) MultiByteToWideChar(CP_UTF8, 0, utf8, -1, ptr, len);
utf16.ReleaseBuffer();
}
return utf16;
}
and getting errors...
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'const class CString' to 'const char *'
Mohibur Rashid 28-Jan-13 5:06am    
Why did you CStringA to CString and CStringW to CString?
Mr Sam 28-Jan-13 5:10am    
Giving error like: error C2501: 'CStringW' : missing storage-class or type specifiers
See the tip Handling simple text files in C/C++[^] here at CodeProject.

For a Unicode application, perform these steps:

  • Determine the file size.
  • Allocate a buffer of that size plus 1 [EDIT]2 bytes[/EDIT] for the terminating NULL character.
  • Read the file content into the buffer.
  • Terminate the string by setting the last character of the buffer to NULL.
  • Determine the encoding of the file.
  • If the encoding is UTF-16LE, you can use the buffer as it is.
  • Otherwise, convert the buffer content to Unicode (UTF-16LE) using MultiByteToWideChar() (call first with destination buffer and size set to NULL to get the required Unicode string length, allocate the Unicode string buffer, and perform the conversion).
  • Delete allocated buffers when not needed anymore (e.g. the file buffer after content has been converted to Unicode).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900