Click here to Skip to main content
13,050,617 members (92,020 online)
Rate this:
Please Sign up or sign in to vote.
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

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

1 solution

Rate this: bad
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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 30 Aug 2011
Copyright © CodeProject, 1999-2017
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