赞
踩
上一篇,介绍了写文件,这一篇我要介绍的如何读取文件内容并显示出来
这里假设VC++的字符集编码格式是Unicode,要显示的文本内容是strResult
1、读取ANSI编码格式的文本文件
- CStdioFile file;
- if (!file.Open(strFileName, CFile::modeRead | CFile::typeBinary))
- return;
-
- // 读取文件内容到CStringA对象中
- CStringA ansiString;
- ULONGLONG nLength = file.GetLength();
- LPSTR lpszString = ansiString.GetBufferSetLength(nLength + 1);
- file.Read(lpszString, (UINT)nLength);
- lpszString[nLength] = '\0';
- ansiString.ReleaseBuffer();
- file.Close();
-
- USES_CONVERSION;
- CString strResult = A2W(ansiString);
2、读取utf-8编码格式的文本文件
- CStdioFile file;
- if (!file.Open(strFileName, CFile::modeRead | CFile::typeBinary))
- return;
-
- //获取文件头
- unsigned char bom[3] = { 0 };
- file.Read(bom, 3);
-
- // 判断文件类型
- if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF)
- {
- //utf8 0xEF, 0xBB, 0xBF
- CStringA utf8String;
- ULONGLONG nLength = file.GetLength() - 3;
- LPSTR lpszString = utf8String.GetBufferSetLength(nLength);
- file.Read(lpszString, (UINT)nLength);
- utf8String.ReleaseBuffer();
- file.Close();
-
- // 获取UTF-8字符串的长度(以字节为单位)
- int utf8Length = utf8String.GetLength();
-
- // 获取转换为Unicode所需的缓冲区大小(以字符为单位)
- int unicodeBufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8String, utf8Length, NULL, 0);
-
- // 分配足够大的缓冲区来保存转换后的Unicode字符串
- wchar_t* unicodeBuffer = new wchar_t[unicodeBufferSize + 1];
-
- // 将UTF-8字符串转换为Unicode编码
- MultiByteToWideChar(CP_UTF8, 0, utf8String, utf8Length, unicodeBuffer, unicodeBufferSize);
-
- // 确保Unicode字符串以null结尾
- unicodeBuffer[unicodeBufferSize] = L'\0';
-
- // 将Unicode字符串存储在一个CStringW对象中
- CString strResult(unicodeBuffer);
- }
3、读取unicode-le编码格式的文本文件
- CStdioFile file;
- if (!file.Open(strFileName, CFile::modeRead | CFile::typeBinary))
- return;
-
- //获取文件头
- unsigned char bom[2] = { 0 };
- file.Read(bom, 2);
-
- // 判断文件类型
- if (bom[0] == 0xFF && bom[1] == 0xFE)
- {
- CString strResult;
- ULONGLONG nLength = (file.GetLength() - 2);
- LPWSTR lpszString = strResult.GetBufferSetLength(nLength + 1);
- file.Read(lpszString, (UINT)nLength);
- lpszString[nLength / 2] = '\0';
- strResult.ReleaseBuffer();
- file.Close();
- }
4、读取unicode-be编码格式的文本文件
- CStdioFile file;
- if (!file.Open(strFileName, CFile::modeRead | CFile::typeBinary))
- return;
-
- //获取文件头
- unsigned char bom[2] = { 0 };
- file.Read(bom, 2);
-
- // 判断文件类型
- if (bom[0] == 0xFE && bom[1] == 0xFF)
- {
- CString unicodeString;
- ULONGLONG nLength = (file.GetLength() - 2);
- LPWSTR lpszString = unicodeString.GetBufferSetLength(nLength + 1);
- file.Read(lpszString, (UINT)nLength);
- lpszString[nLength / 2] = '\0';
- unicodeString.ReleaseBuffer();
- file.Close();
-
- // 获取Unicode-LE编码字符串的长度(以字符为单位)
- int unicodeLELength = unicodeString.GetLength();
-
- // 分配足够大的缓冲区来存储转换后的Unicode-BE编码字符串
- wchar_t* unicodeLEBuffer = new wchar_t[unicodeLELength];
-
- // 将Unicode-LE编码字符串的每个字符转换为Unicode-BE编码,并写入新的缓冲区中
- for (int i = 0; i < unicodeLELength; i++) {
- wchar_t ch = unicodeString[i];
- unsigned char highByte = (ch >> 8) & 0xFF;
- unsigned char lowByte = ch & 0xFF;
-
- unicodeLEBuffer[i] = (lowByte << 8) | highByte;
- }
-
- // 创建一个新的CString对象,并将转换后的Unicode-BE编码字符串分配给它
- CString strResult(unicodeLEBuffer, unicodeLELength);
- }
注意事项:如果需要将文本内容显示在界面的CEdit控件里面去,还需要处理一下换行符号,否则文本内容显示不会自动换行,具体代码如下:
- strResult.Replace(_T("\n"), _T("\r\n"));
- SetDlgItemText(IDC_TEXT, strResult);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。