当前位置:首页 » 编程语言 » c语言将txt文件读取计算
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言将txt文件读取计算

发布时间: 2023-03-26 21:35:39

c语言如何实现对txt文件的读取和写入

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

② 用c语言怎么读取txt文件中的行数

读取文件行数, 可以逐个字符读取文件,到文件尾,统计 的个数

参考代码如下

#include<stdio.h>
intmain()
{
intc;
FILE*fp;
intlines=0;
fp=fopen("in.txt","rb");
if(fp)
{
while((c=fgetc(fp))!=EOF)
if(c==' ')lines++;
printf("%d ",lines);
fclose(fp);
}
return0;
}

也可以通过fgets函数,每次读取一行,到文件尾,然后计算读取的次数

#include<stdio.h>
#include<string.h>
intmain()
{
chars[100];
FILE*fp;
intlines=0;
fp=fopen("in.txt","r");
if(fp)
{
while((fgets(s,100,fp))!=NULL)
if(s[strlen(s)-1]==' ')lines++;
printf("%d ",lines);
fclose(fp);
}
return0;
}

③ C语言读取文件数据并进行计算(平均值,最值,0的个数)

#include
int main()
{ int i,j,n=0,n0=0,k=0;
double a[10000],s=0,t;
FILE *fp;
char c;
if(!(fp=fopen("text.txt","r")))
{printf("File open error! ");
return 0;
}
for(;fscanf(fp,"%lf%c",&a[n],&c)!=-1;n++)
{if(a[n]==0)n0++;
if(c==' ')k++;
s+=a[n];
printf("%g ",a[n]); //不需要显示时请把此行删除
}
if(c==32)k++;
fclose(fp);
for(i=0;i<10;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
{t=a[j]; a[j]=a[j+1];a[j+1]=t;}
printf(" 平均值:%g ",s/n);
printf("前10个最大的数:");
for(i=n-1;i>n-11;i--)
printf("%g ",a[i]);
printf(" 0的个数:%d ",n0);
printf("共有%d行 ",k);
return 0;
}

④ 如何用C语言读取TXT格式文件中的数据进行运算

#include<stdio.h>
#include<malloc.h>

voidmain()
{
intn,no,i;
FILE*fp=fopen("data.txt","r);
float*x,*y,result;
if(fp==NULL)return;
fscanf(fp,"%d%d",&n,&no);
x=(float*)malloc(n*sizeof(float);
y=(float*)malloc(n*sizeof(float);
for(i=0;i<n;i++)fscanf("%f%f",x+i,y+i);
result=0.0f;
for(i=0;i<n-1);i++)result+=0.5f*(x[i]*y[i+1]-x[i+1]*y[i]);
printf("%dresultis%f ",no,result);
free(x);
free(y);
fclose(fp);
}

⑤ 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 );

}

(5)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文档中的数据,并带入公式中计算,结果输出并保存为txt

#include<stdio.h>
#include<stdlib.h>
#include<math.h>旦镇姿

int main()
{
FILE *fin, *fout;
int a,b,c,d,s;
fin=fopen("f1.txt","r"); //原始数据文件
fout=fopen("f2.txt","旅判w"); //保存结果文件
fscanf(fin,"%d%d%d%d",&a,&b,&c,&d);
while(!feof(fin)){
s=(int)sqrt((a-b)*(a-b)+(c-d)*(c-d));
fprintf(fout,"模绝%d\n",s);
fscanf(fin,"%d%d%d%d",&a,&b,&c,&d);
}
fclose(fin);
fclose(fout);
}

⑦ 怎样用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);
}

⑧ 用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语言 怎样用文件中读取数据 然后进行计算

主要通过fscanf,fprintf格式化输入输出函数实现,主要代码如下,
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *pf=NULL;
char name[20];//存储用户名
float salary=0;//存储工资
pf=fopen("test.txt", "r+" );//test.txt文件中内容为kobe 90000.80
if(!pf)
{
printf("打开文件失败,程序退出!");
exit(1);
}
fscanf(pf,"%s %f",name,&salary);
salary*=0.9;//处理工资,例如扣除五险、扣税等。
fprintf(pf,"\n%f",salary);//写入test.txt文件中
printf("%s %f\n",name,salary);//输出kobe 81000.718750
if(pf)//关闭文件
{
fclose( pf);
pf=NULL;
}
return 0;
}

int fscanf( FILE *stream, const char *format, ... );函数fscanf()以scanf()的执行方式从给出的文件流中读取数据,
fscanf()的返回值是事实上已赋值的变量的数,如果未进行任何分配时返回EOF。
int fprintf( FILE *stream, const char *format, ... );fprintf()函数根据指定的format(格式)发送参数到由stream指定的文件。fprintf()只能和printf()一样工作,
fprintf()的返回值是输出的字符数,发生错误时返回一个负值。

⑩ C语言如何读取文本文档里的数据并计算

#include<iostream>
usingnamespacestd;
intmain()
{
freopen("testin.txt","r",stdin);
freopen("testout.txt","w",stdout);
intn;
cin>>n;
cout<<n<<endl;
fclose(stdin);
fclose(stdout);
return0;
}

如下图片: