急!!!读文件问题!!

xiaohyy 2003-03-06 08:20:34
读一个文本文件,其中有汉字、字母、符号等,我从文件中读取数据出来,汉字全是乱码,比如文本文件中的内容为“小小小#大大大12#”,我要判#之间的字符串,请问怎样实现,我的系统是win2k。。。
...全文
116 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
monkey_boy 2003-03-07
  • 打赏
  • 举报
回复
CString m_strFileName="C:\\1.txt";
CString strSubString="#";
CString strRet;
if(LoadText(m_strFileName,strSubString,strRet))
AfxMessageBox(strRet);

BOOL CTestTodayView::LoadText(CString strFileName, CString strSubString,CString& strReturn)
{
HANDLE hFile;
CString strBuffer, strTempBuffer;
DWORD dwSize, dwBytesRead;
BOOL bRet = TRUE;
INT nIndexBegin, nIndexEnd;

strReturn.Empty();

try
{
// 打开文件
hFile = CreateFile( strFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL );
if( hFile == NULL )
AfxThrowUserException();

// 读文件内容
dwSize = GetFileSize( hFile, NULL );
if( !ReadFile( hFile, strBuffer.GetBuffer( dwSize ), dwSize, &dwBytesRead, NULL ) )
AfxThrowUserException();
strBuffer.ReleaseBuffer();
strTempBuffer = strBuffer;

// 分析文件内容
nIndexBegin = strTempBuffer.Find( strSubString ); //寻找"#"的起始位置
if( nIndexBegin == -1 )
AfxThrowUserException();

nIndexEnd=strTempBuffer.Find( strSubString, nIndexBegin+1 ); //寻找"#"的结束位置
if( nIndexEnd == -1 )
AfxThrowUserException();

nIndexBegin=nIndexBegin+1; //截取字符串的起始位置
strReturn=strTempBuffer.Mid(nIndexBegin,nIndexEnd-nIndexBegin);
}
catch( ... )
{
bRet = FALSE;
}

// 关闭文件
CloseHandle( hFile );

return bRet;
}
snsins 2003-03-06
  • 打赏
  • 举报
回复
先取得file的长度
然后用ReadFile一下就可以读到buffer里去了
xiaohyy 2003-03-06
  • 打赏
  • 举报
回复
to : jennyvenus()
修正一下,我的意思是
我必须把file中的文本一次性全部读出来。。。
xiaohyy 2003-03-06
  • 打赏
  • 举报
回复
to : jennyvenus()
按照你的做法,我必须把file中的文本全部读出来。。。
用户 昵称 2003-03-06
  • 打赏
  • 举报
回复
读取整行用fgets
用户 昵称 2003-03-06
  • 打赏
  • 举报
回复
use strtok function

/* Compile options needed: none
*/

#include <string.h>
#include <stdio.h>

char *string = "a string,of ,,tokens";
char *token;

void main(void)
{
token = strtok(string," ,"); /*There are two delimiters here*/
while (token != NULL){
printf("The token is: %s\n", token);
token = strtok(NULL," ,");
}
}
The output of this program is as follows:
The token is: a


The token is: string


The token is: of


The token is: tokens
xiaohyy 2003-03-06
  • 打赏
  • 举报
回复
其实也就是写一个类似CStdioFile::ReadString()的函数,只是不是用回车和换行符区分,而是用其他符号(比如说‘#’)区分。。
xiaohyy 2003-03-06
  • 打赏
  • 举报
回复
我的做法是一个一个字符地读,可以其中的汉字部分是乱码,win2k是unicode编码,应该不会存在这种问题啊,还有我调用file.GetLength()返回的字节数1个汉字是2,1个字母是1。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧