当前位置:首页 » 编程语言 » c语言读取txt数据

c语言读取txt数据

发布时间: 2022-05-26 06:01:36

‘壹’ 用c语言读取一个txt文件

如果预知前面的是英文后面的是中文,即可分开:

#include<stdio.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]); n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

如果中英文顺序不一定,且不会有中英文混合单词:

#include<stdio.h>

#include<string.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]);

if ( y[n][0]<0 ) { strcpy(s,y[n]);strcpy(y[n],h[n]);strcpy(h[n],s); } //汉字字符ASCII码小于0

n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("无法打开文件读取。 ");

}

‘贰’ C语言如何实现对txt文件的读取和写入

1、使用VS新建空工程,直接点击确定,如下所示。

‘叁’ c语言 如何读取txt内容到数组

#include<stdio.h>

#include<stdlib.h>

#define N 10000

int main()

{

FILE *fp;

if((fp=fopen("D:\123.txt","r"))==NULL)//判断文件是否打开成功

{//读取D盘下,名为123的文本文件

printf("文件打开失败 ");

exit(0);

}

else

printf("文件打开成功 ");

double a[N];

for(int i=0;i<5;i++)//读取五个浮点型数据

fscanf(fp,"%lf,",&a[i]);//fscanf函数固定格式读取文本中的数据;

for(int i=0;i<5;i++)

printf("%.6lf, ",a[i]);

fclose(fp);

}

‘肆’ 在c语言中,如何读取一个txt文件中的信息

一般来说在C语言中读取txt文件的信息有两种方法,一种是使用C语言标准文件I/O中的fopen()、fread()等等函数,一种是调用操作系统中的API函数,比如Windows上的ReadFile()、OpenFile()等等,现在操作系统一般都具备内存文件映射功能,对于大的txt文件,一般都使用这种方式操作。下面是一个使用C语言标准文件I/O操作文件的例子。

#include<stdio.h>

FILE*stream;

voidmain(void)
{
longl;
floatfp;
chars[81];
charc;

stream=fopen("fscanf.out","w+");
if(stream==NULL)
printf("Thefilefscanf.outwasnotopened ");
else
{
fprintf(stream,"%s%ld%f%c","helloworld",
65000,3.14159,'x');

/*Setpointertobeginningoffile:*/
fseek(stream,0L,SEEK_SET);

/*Readdatabackfromfile:*/
fscanf(stream,"%s",s);
fscanf(stream,"%ld",&l);

fscanf(stream,"%f",&fp);
fscanf(stream,"%c",&c);

/*Outputdataread:*/
printf("%s ",s);
printf("%ld ",l);
printf("%f ",fp);
printf("%c ",c);

fclose(stream);
}
}

‘伍’ C语言 读取TXT文档

应该是m_strFileName不支持中文字符造成的,你试试将文件名改为英文的,再赋值给m_strFileName试试。

‘陆’ c语言读取txt文件里面的数据

在C语言中,文件操作都是由库函数来完成的。
要读取一个txt文件,首先要使用文件打开函数fopen()。
fopen函数用来打开一个文件,其调用的一般形式为: 文件指针名=fopen(文件名,使用文件方式) 其中,“文件指针名”必须是被说明为FILE 类型的指针变量,“文件名”是被打开文件的文件名。 “使用文件方式”是指文件的类型和操作要求。“文件名”是字符串常量或字符串数组。
其次,使用文件读写函数读取文件。
在C语言中提供了多种文件读写的函数:
·字符读写函数 :fgetc和fputc
·字符串读写函数:fgets和fputs
·数据块读写函数:freed和fwrite
·格式化读写函数:fscanf和fprinf
最后,在文件读取结束要使用文件关闭函数fclose()关闭文件。

下面以格式化读写函数fscanf和fprintf为例,实现对文件A.txt(各项信息以空格分割)的读取,并将它的信息以新的格式(用制表符分割各项信息)写入B.txt,实现对A.txt的处理。

C语言源程序如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct student{
char name[32];
int no;
char sex[16];
float score;
} stu;

int main(int argc, char* argv[])
{
//打开文件
FILE * r=fopen("A.txt","r");
assert(r!=NULL);
FILE * w=fopen("B.txt","w");
assert(w!=NULL);

//读写文件
stu a[128];
int i=0;
while(fscanf(r,"%s%d%s%f",a[i].name,&a[i].no,a[i].sex,&a[i].score)!=EOF)
{
printf("%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到显示器屏幕
fprintf(w,"%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//输出到文件B.txt
i++;
}

//关闭文件
fclose(r);
fclose(w);

system("pause");
return 0;
}

‘柒’ 如何用c语言从txt文件中读取数据

//其中的in.txt就是你要读取数据的文件,当然把它和程序放在同一目录
-------------------------------------
#include
<stdio.h>
int
main()
{
int
data;
file
*fp=fopen("in.txt","r");
if(!fp)
{
printf("can't
open
file\n");
return
-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return
0;
}

‘捌’ 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 );

}

(8)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] = '';

printf( "%s ", buffer );

fclose( stream );

}

‘玖’ 怎样用C语言从txt文件中读入数据

1 以fopen打开文件,使用"r"方式。

2 通过fscanf,按照文件中的数据格式,读入数据。

3 关闭文件并使用数据。

如文件in.txt中存在三个以空格分隔的数据,依次为整型,字符串,以及浮点型,则读取数据的代码可以写作:

intmain()
{
FILE*fp;
inta;
chars[100];
floatf;
fp=fopen("in.txt","r");
if(fp==NULL)return-1;//打开文件失败,结束程序。
fscanf(fp,"%d%s%f",&a,s,&f);
fclose(fp);

printf("readvalue:%d,%s,%f",a,s,f);
}
热点内容
pow在c语言中什么 发布:2025-02-12 21:07:24 浏览:320
php查询mysql连接 发布:2025-02-12 21:05:45 浏览:632
linuxc当前时间 发布:2025-02-12 21:03:32 浏览:28
云锁神服务器 发布:2025-02-12 21:03:29 浏览:487
c语言int和float 发布:2025-02-12 21:03:24 浏览:20
我的世界有什么好玩的大服务器 发布:2025-02-12 21:01:59 浏览:98
方舟手游如何解锁自己的服务器 发布:2025-02-12 20:54:09 浏览:657
猫影视源码 发布:2025-02-12 20:42:05 浏览:923
局域网如何访问其他电脑 发布:2025-02-12 20:39:06 浏览:378
新平板电脑的数字密码如何知道 发布:2025-02-12 20:31:19 浏览:345