VC6文本文件:使用CStdioFile读取ANSI和unicode类型的文本文件 [英] VC6 Text Files: Reading a text file of type ANSI and unicode using CStdioFile

查看:527
本文介绍了VC6文本文件:使用CStdioFile读取ANSI和unicode类型的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我将VC .net项目从使用多字节字符集"转换为使用Unicode字符集"(即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)
{

}




一旦我将项目更改为Unicode支持.使用上面的代码,我正在尝试读取Unicode文本文件,它工作正常.如果我尝试读取ANSI文本文件,则无法正确读取.一旦我使用Unicode支持构建项目,就会出现此问题.

是否可以使用CStdioFile读取ANSI和unicode类型的文本文件?还是我做错了什么?我的要求是,我需要使用上述代码读取ANSI和Unicode类型的文本文件.



任何帮助是极大的赞赏.谢谢.


[原始标题]
是否可以使用CStdioFile读取ANSI和unicode类型的文本文件?还是我做错了什么?




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?

推荐答案

在UNICODE版本中读取ANSI文件时,您首先需要将数据读取到一个char缓冲区,然后将文本转换为UNICODE字符串以进行进一步处理.

请注意,您的代码无法处理文件中可能存在的BOM(字节顺序标记).此文章 [写作使用CStdioFile到UNICODE文件 [ ^ ]出现问题.
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.


这篇关于VC6文本文件:使用CStdioFile读取ANSI和unicode类型的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆