How to read Unicode text files

16 04 2013

Recently I met with a problem when I wrote some codes to read a file using MFC API CStdioFile. But unfortunately it failed to read UNICODE files :(. Then only I came to know that the MFC classes CFile and CStdioFile can read/write text files only in ANSI format. So how we will resolve it?

Windows provides another API fopen_s for Unicode file streams. This API supports encoding flag, where we cab mention the desired flag while reading a file or writing a file.

fopen_s(&fStream, “UnicodeFile.txt”, “r,ccs=encoding”);

BOOL ReadUnicodeFile()
    CString strFileName = _T("C:\\UnicodeFile.txt");
    FILE *fStream;
    errno_t errCode = _tfopen_s(&fStream, strFileName, _T("r, ccs=UNICODE"));
    if (0 != errCode)
        return FALSE;
    CStdioFile File(fStream);
    CString strLine;
        //reading line-by-line

Specify the coded character set to use (ANSI, UTF-8, UTF-16LE, and UNICODE) for this file. This option is available in Visual C++ 2005 and later.




3 responses

14 08 2013
Home Page

Rather useful look forth to coming back.

25 09 2013
web site

Hi there, I log on to your blog like every week. Your writing style is awesome, keep doing what
you’re doing!

27 09 2013
Sanoop S P

Thank you

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: