Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C MFC
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
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 27-Jan-13 19:14pm
Mr Sam679
Edited 27-Jan-13 20:12pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if your file is ANSI then read as ANSI
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;
}
  Permalink  
v2
Comments
Mr Sam at 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 at 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 at 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 at 28-Jan-13 5:06am
   
Why did you CStringA to CString and CStringW to CString?
Mr Sam at 28-Jan-13 5:10am
   
Giving error like: error C2501: 'CStringW' : missing storage-class or type specifiers
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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).
  Permalink  
v2

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

  Print Answers RSS
0 CHill60 280
1 Sarvesh Kumar Gupta 238
2 Sergey Alexandrovich Kryukov 203
3 OriginalGriff 163
4 DamithSL 153


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid