怎样读入中文文本?

zhucde 2003-05-20 08:23:56
怎样从一个.txt文件中读入中文的文本?
渴望代码!
...全文
45 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
oiq 2003-05-20
  • 打赏
  • 举报
回复
教你一绝活:
用系统映象文件的方法读取这个文件,映象文件初始化后,你可以得到指向文件头的
一个指针,有了指针,操作这个文件就可以象操作内存数据一样了。

接下来一个字符一个字符读取,遇到字符值大于128,则判断这肯定是一个汉字的前
半部分,所以我们读取二个字符,这样就把一个汉字读取了。然后NEXT,判断下一个字符
的值。
Fansy2000 2003-05-20
  • 打赏
  • 举报
回复
//use C++ iostream

char *chError = (char *)new(char[50]);
ifstream input;
int cStrBuffer[1]; //read_buffer

input.open(chFile1,ios::in);//the second parameter means istream
if(input.fail())
{
chError = "Error in opening the file:";
strcat(chError,chFile1);
AfxMessageBox(chError);
exit(1);
}
input.read((unsigned char *)&cStrBuffer,1);//the second parameter means
//how many bytes you want to read
zhucde 2003-05-20
  • 打赏
  • 举报
回复
楼上,能否留下你的QQ号?我的是:22343512
shilong 2003-05-20
  • 打赏
  • 举报
回复

str=txtbuffer
把str指向txtbuffer[2048]这段内存

而char txtbuffer[2048];
是局部变量,当函数结束时,它就无郊了
zhucde 2003-05-20
  • 打赏
  • 举报
回复
OK了,不过这什么加了str=txtbuffer就不行了呢?它们不是同一个类型吗?
shilong 2003-05-20
  • 打赏
  • 举报
回复
或者
int LoadChTxt(char *str)
{
CFile myfile;
myfile.Open("data/document.txt",CFile::modeRead);
char txtbuffer[2048];
myfile.Read(txtbuffer,2048);
strcpy(str,txtbuffer);
return 1;
}

shilong 2003-05-20
  • 打赏
  • 举报
回复
int LoadChTxt(char *str)
{
CFile myfile;
myfile.Open("data/document.txt",CFile::modeRead);
myfile.Read(str,2048);
return 1;
}

zhucde 2003-05-20
  • 打赏
  • 举报
回复
int LoadChTxt(char *str)
{
CFile myfile;
myfile.Open("data/document.txt",CFile::modeRead);
char txtbuffer[2048];
myfile.Read(txtbuffer,2048);
str=txtbuffer;
return 1;
}


int CMyBmpDlgDlg::OnButton1()
{

CAboutDlg Dlg;
Dlg.DoModal();
char ttt[2084];
if(!LoadChTxt(ttt))
return 0;
GetDlgItem(IDC_STATIC)->SetWindowText(ttt);
Invalidate();
return 1;
}
shilong 2003-05-20
  • 打赏
  • 举报
回复
把你的代码贴上来~
zhucde 2003-05-20
  • 打赏
  • 举报
回复
为何不管中文还是英文,读出来的都是是"h?"?再无其它内容了?
zhujianping_es 2003-05-20
  • 打赏
  • 举报
回复
我刚刚编了一个中文的搜所算法,建议如果你对中文进行编辑,查找的话,最好还是用
UNICODE编码.see more about
wchar_t,and BSTR
and it is not so hard!
lingfeng8888 2003-05-20
  • 打赏
  • 举报
回复
重新给你一段代码。
CFile myfile;
myfile.Open("your.txt",CFile::modeRead);
CString txtbuffer;
int filelength = myfile.GetLength();
myfile.Read(txtbuffer,filelength);
myfile.Close;
txtbuffer即是读入的文本.这样保证不会有截断的现象。

实际上, shilong(银羽(★★倒计时250分)) 已经很完美了。
shilong 2003-05-20
  • 打赏
  • 举报
回复
不会~
ReadString每读一次都是遇到'\n'才停
tianhua 2003-05-20
  • 打赏
  • 举报
回复
这样会不会遇上截断的问题,就是说一个中文会不会只读到它的一半编码?
shilong 2003-05-20
  • 打赏
  • 举报
回复
CStdioFile file;
CString str;
file.Open("文件名.txt",CFile::modeRead);
file.ReadString(str);
file.Close;
MessageBox(str);
lingfeng8888 2003-05-20
  • 打赏
  • 举报
回复
CFile myfile;
myfile.Open("your.txt",CFile::modeRead);
char txtbuffer[2048];
myfile.Read(txtbuffer,2048);

txtbuffer即是读入的文本。英文和中文均可。

16,471

社区成员

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

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

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