1. c语言如何读取TXT中的中文字符串
#include<stdio.h>
intmain(void)
{
FILE*p;
charch;
p=fopen("a.txt","r");
while(!feof(p))
{
if(fgetc(p)=='>')
{
while((ch=fgetc(p))!='<')
{
putchar(ch);
}
printf(" ");
}
}
fclose(p);
return0;
}
//昨天我说了没编译估计会出错,这次没事了运行通过
//还有只能读英文,乔丹不行,这是因为c编译器不支持unicode编码,char类型的是只能表示128个字符,Unicode是两个字节,可以表示汉字。
2. c语言读取txt文件内容
用C语言从txt文件中读取数据,可以使用C标准库文件自带的文件接口函数进行操作。
一、打开文件:
FILE *fopen(const char *filename, const char *mode);
因为txt文件为文本文件, 所以打开时选择的mode应为"r"或者"rt"。
二、读取文件:
读取文件应根据文件内容的格式,以及程序要求,选择读取文件的函数。可以使用一种,也可以几种混用。 常用的文件读取函数如下:
1、fgetc, 从文件中读取一个字节并返回。 适用于逐个字节读取。
2、 fgets, 从文件中读取一行。适用于整行读取。
3、fscanf, 格式化读取文件, 在已经清楚文件存储格式下,可以直接用fscanf把文件数据读取到对应类型的变量中。
4、fread, 整块读取文件, 对于txt文件比较少用。
三、关闭文件:
读取结束后,应调用fclose函数关闭文件。
3. C语言如何从txt文件中读入汉字并将其存在字符串数组中...
程序代码:
#include<stdio.h>
#include<stdlib.h>
#defineMAXSIZE1024
intmain(intargc,char*argv[])
{
FILE*fp;
charbuf[MAXSIZE];
fp=fopen("./hanzi.txt","r");
if(fp==NULL)
{
perror("calltofopen!");
exit(1);
}
fread(buf,1,MAXSIZE,fp);
printf("%s ",buf);
return0;
}
附hanzi.txt文件内容:
你好,很高兴认识你!希望你的人生的每一天都会像今夜的星辰一样,灿烂无比!
运行结果截图:
4. 怎么用c语言读取txt中的中文信息
用fscanf(%s)或者fgets都可以读,中文就是一个普通的字符串。
5. 用c语言从txt文件中读取中文并显示出来
很简单的一个功能,现在写的是将源文件写死的
#include"stdafx.h"
intmain(void)
{
charfilename;
FILE*file=fopen("F:\wz-mac.txt","r");//打开文件
while((filename=fgetc(file))!=EOF)
{
printf("%c",filename);//打印内容到屏幕
}
return0;
}
6. C语言从txt中读取汉字到数组
汉字在ANSI/GBK或UCS-2/UTF-16编码中占2个字节,在UTF-8中占3个字节,在UCS-4/UTF-32中占4个字节。如果确定编码(默认一般是ANSI/GBK)的话,就可以判断出一个汉字的大小。由于fgetc只能处理单字节数据,且char无法存放超过一个字节,因此应该把汉字(无论是几个)作为数组处理。具体可以用fread(s,n,1,fp);来从文件fp读入指定的n字节到字符串s中。也可以用fscanf配合%s使用。读入一行的话,可以考虑fgets。
对于宽字符而言,可以用wchar_t代替char,用fgetwc代替fgetc。不过wchar_t和对应函数的行为是平台相关的,一般也不适合ANSI/GBK兼容窄字符串处理。
====
[原创回答团]
7. C语言如何读取txt文本里面的内容
C语言可以使用fopen()函数读取txt文本里。
示例:
#include <stdio.h>
FILE *stream, *stream2;
void main( void )
{
int numclosed;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = fopen( "data", "r" )) == NULL )
printf( "The file 'data' was not opened " );
else
printf( "The file 'data' was opened " );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened " );
else
printf( "The file 'data2' was opened " );
/* Close stream */
if(fclose( stream2 ))
printf( "The file 'data2' was not closed " );
/* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u ", numclosed );
}
(7)c语言从txt读取中文扩展阅读
使用fgetc函数
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
FILE *stream;
char buffer[81];
int i, ch;
/* Open file to read line from: */
if( (stream = fopen( "fgetc.c", "r" )) == NULL )
exit( 0 );
/* Read in first 80 characters and place them in "buffer": */
ch = fgetc( stream );
for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )
{
buffer[i] = (char)ch;
ch = fgetc( stream );
}
/* Add null to end string */
buffer[i] = '