怎样读取TXT文本文件中的汉字(C&C++)

pingpeace 2008-04-20 04:20:10
如题,请教高手,C/C++ 中怎样把一个文本文件中的汉字读取出来并输出到另一个文本文件中,C/C++用什么函数可以读取文本文件中的汉字?希望高手给予指点,能给出个例子更好,谢谢!!
...全文
1886 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang541880 2012-06-15
  • 打赏
  • 举报
回复
是在什么平台哦?
kingstarer 2008-04-30
  • 打赏
  • 举报
回复
LZ运行一下这段代码,看看输出,你就应该知道怎么读了

char *str = "例";
putchar(str[0]);
putchar(str[1]);
meiZiNick 2008-04-30
  • 打赏
  • 举报
回复
接分先!
zhongyuanceshi 2008-04-22
  • 打赏
  • 举报
回复
用UE打开,读取汉字得二进制,保存在一数组中,然后读取该数组存储到另一文件中
Mr_Huge 2008-04-20
  • 打赏
  • 举报
回复
测试结果可以把汉字从其他字母及数字中提取出来,但最后会多出个ff.
Mr_Huge 2008-04-20
  • 打赏
  • 举报
回复
#include <fstream.h>
int main()
{
unsigned char c;
ifstream infile("t1.txt");
ofstream outfile("t2.txt");

int flag = 0;
if(infile.eof()) return 1;

do
{
infile.get(c);
if(flag==1)
{
outfile << c;
flag = 0;
}
else if(c>0xa0)
{
outfile << c;
flag = 1;
}
}
while(!infile.eof());

infile.close();
outfile.close();
return 0;
}
用这个试试。
pingpeace 2008-04-20
  • 打赏
  • 举报
回复
C++里面好像不用自己设定条件判断,他会自动识别,只要是在装有汉语支持的环境中就可以。
Mr_Huge 2008-04-20
  • 打赏
  • 举报
回复
为什么都没有判断汉字的语句?
Yun0825 2008-04-20
  • 打赏
  • 举报
回复
....缓冲区大小为什么是128?
Yun0825 2008-04-20
  • 打赏
  • 举报
回复

while (!feof(in_file)) {
char buffer[128]; //Ϊʲô���ȶ���Ϊ128???
fgets(buffer, sizeof(buffer), in_file);
fputs(buffer, out_file);
}
zhaoyg1986 2008-04-20
  • 打赏
  • 举报
回复
5楼的朋友你的头像是哪来的?
pingpeace 2008-04-20
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream is("C://intest.txt");
ofstream os("C://outtest.txt");
char delim;
while (is.get(delim))
{
os << delim;

}
return 0;
}


我用上面C++写法也是可以实现的,但想了解一下用C的话怎么实现,3楼的方法不错。
pingpeace 2008-04-20
  • 打赏
  • 举报
回复
可否指点一下,用wchar / wstring 怎么写?能否给个例子?
skran 2008-04-20
  • 打赏
  • 举报
回复
其实很简单
#include<fstream.h>
int main()
{
char c;
ifstream infile("t1.txt");
ofstream outfile("t2.txt");
while(!infile.eof()&&!outfile.eof())
{infile.get(c);outfile.put(c);}
infile.close();
outfile.close();
return 0;
}

t1.txt是你新建的文档,在里面随便输入一些汉字;t2.txt是自动生成的
Inhibitory 2008-04-20
  • 打赏
  • 举报
回复
汉字在文件中也还是以二进制形式存储的,
needalfan 2008-04-20
  • 打赏
  • 举报
回复
#include <wchar.h>
#include <stdio.h>
#include <locale.h>
int main()
{
FILE *handleread;
wint_t word;
setlocale(LC_ALL,"chs");
handleread=_wfopen(L"d:\\1.txt",L"r");
if(handleread==NULL)
{
printf("文件读取失败.");
return 0;
}
word=fgetwc(handleread);
printf("第一个汉字:%C\n",word);
fclose(handleread);
return 0;
}



在d 盘放一个文件,输入一个汉字的试试看.

满意给点分的.没分下载啊.....
星羽 2008-04-20
  • 打赏
  • 举报
回复


#include "stdlib.h"

int main() {

FILE* in_file = fopen("in.txt", "r");
FILE* out_file = fopen("out.txt", "w");

if (!in_file) {
fclose(out_file);
return 1;
}

while (!feof(in_file)) {
char buffer[128];
fgets(buffer, sizeof(buffer), in_file);
fputs(buffer, out_file);
}

fclose(out_file);
fclose(in_file);

return 0;
}
fallening 2008-04-20
  • 打赏
  • 举报
回复
use wchar or wstring
HelloDan 2008-04-20
  • 打赏
  • 举报
回复
string str;
ifstream infile("filename");

getline(str,infile);

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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