Click here to Skip to main content
15,889,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C++
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
Updated 29-Aug-11 23:56pm
v4
Comments
Slacker007 30-Aug-11 5:56am    
Edited for readability, code block, and tags.

1 solution

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.
 
Share this answer
 

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