Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 Unicode ANSI
Actually I converted my VC .net project from Use Multi-Byte Character Set to Use Unicode Character Set (i.e. Unicode support). Then lot of build errors fixed.
 
Before in my project, I’m reading the ANSI text file as below
 
CStdioFile l_oStdioFile;
CString l_strValue = _T("");
TCHAR l_chBuff[1024*10];
CFileException l_oFileEx;
 
CString m_strFileName = _T("C:\\Documents and Settings\\PHari\\Desktop\\ASAS.txt");
 
BOOL l_bResult = false;
 
try
{
     l_bResult = l_oStdioFile.Open(m_strFileName,CFile::modeRead,&l_oFileEx);
     int count = l_oStdioFile.GetLength();
 
     if(!l_bResult) return;
 
     UINT l_nBytesRead = l_oStdioFile.Read(l_chBuff,1024*10);
 
    l_oStdioFile.Close();
 
    int l_nIndex = 0;          
 
    for(int l_nCount = 0; l_nCount < (int)l_nBytesRead; l_nCount++)
    {
        l_strValue += l_chBuff[l_nCount];
    }
 
 }
catch(CFileException oFileEx)
{
 
}
 

Once I changed my project to Unicode support. With the above code, I’m trying to read the Unicode text file, it is working fine. If I try to read the ANSI text file, it is not reading properly. This problem is coming Once I built my project with Unicode support.
 
Is it possible to read text file of type ANSI and unicode using CStdioFile as shown above? Or where I’m doing wrong? My requirement is I need to read the text file of type ANSI and Unicode with above code.
 

 
Any help is greatly appreciated. Thanks.
 

[orig. title]
Is it possible to read text file of type ANSI and unicode using CStdioFile as shown above? Or where I’m doing wrong?
Posted 30-Aug-11 0:21am
Edited 30-Aug-11 0:56am
Slacker00775.8K
v4
Comments
Slacker007 at 30-Aug-11 5:56am
   
Edited for readability, code block, and tags.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

When reading the ANSI file in a UNICODE build you first need to read the data into a char buffer and then convert the text into a UNICODE string for further processing.
 
Please note that your code does not handle the possible presence of a BOM (Byte Order Mark) in the file. This article[^] on reading and writing UNICODE files might be useful.
 
Do not use CStdioFile for writing to UNICODE file[^] it has problems.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 30 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100