當前位置:首頁 » 編程語言 » 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;
}

如下圖片: