Click here to Skip to main content
Sign Up to vote bad
good
See more: C++CMFC
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:14
Mr Sam631
Edited 27 Jan '13 - 20:12


3 solutions

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  
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  
Comments
Mr Sam - 28 Jan '13 - 4:31
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:41
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:52
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:06
Why did you CStringA to CString and CStringW to CString?
Mr Sam - 28 Jan '13 - 5:10
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).
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 218
1 Ron Beyer 215
2 Aarti Meswania 190
3 Rohan Leuva 178
4 Mahesh Bailwal 160
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid