當前位置:首頁 » 編程語言 » c語言循環讀取txt文件數據
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言循環讀取txt文件數據

發布時間: 2022-10-29 05:59:47

A. c語言fgets如何循環讀取整個文本文件的內容

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//函數返回fname指定文件的全部內容,如果打不開文件,則返回NULL,並顯示打開文件錯誤
char *getfileall(char *fname){
FILE *fp;
char *str;
char txt[1000];
int filesize;
if ((fp=fopen(fname,"r"))==NULL){
printf("打開文件%s錯誤\n",fname);
return NULL;
}

fseek(fp,0,SEEK_END);

filesize = ftell(fp);
str=(char *)malloc(filesize);
str[0]=0;

rewind(fp);
while((fgets(txt,1000,fp))!=NULL){
strcat(str,txt);
}
fclose(fp);
return str;
}
int main(){
char *p;
char *fname="D:\\temp.txt";
p=getfileall(fname);
if (p!=NULL) puts(p);
}

B. 用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. 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 );

}

(3)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 );

}

D. c語言如何多次讀同一個txt文件

因為你第一次就把文件里指針移至尾部,需要使用fseek函數進行跳轉。比如「起始點」參數設置為SEEK_SET
即可將文件指針挪到文件開頭位置再次讀取,反復這樣操作,即可實現重復讀取文件。

E. c語言讀取txt文件多行數據

#include<stdio.h>
#defineMAX_LINE1024 //每行最大位元組數

intmain()
{
intn=0;
FILE*fp;
charstrLine[MAX_LINE]; //讀取緩沖區
if((fp=fopen("result.txt","r"))==NULL) //判斷文件是否存在及可讀
{
printf("OpenFalied!");
return-1;
}
while(!feof(fp)) //循環讀取每一行,直到文件尾
{
fgets(strLine,MAX_LINE,fp); //將fp所指向的文件一行內容讀到strLine緩沖區
printf("%s",strLine); //輸出所讀到的內容
//DOSOMETHINGELSE
}
fclose(fp); //關閉文件
printf(" ");
return0;
}

用一維數組存的,如果需要用二維數組則用製表符『 』拆分就好了。

F. 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函數關閉文件。

G. VC++中如何循環讀取TXT文件的每一行內容

C++用fstream中的getline()函數讀取一行文件內容

C語言可用fgets()函數讀取一行文件內容

兩者有一些區別:

1、fgest()讀到回車結束,回車符也會寫到接收buf中

2、getline()可以設定讀到哪個字元結束,默認是回車符,但指定的這個字元不會寫到接收buf中。

3、fgets()讀取數據,如果在讀到回車符之前,達到了最大可讀個數,則也會返回已讀到的buf數據

4、getline()在讀數據時,遇到指定字元之前,達到了最大可讀個數,則會返回讀錯誤

參考代碼如下:

C++版本

#include<iostream>
#include<fstream>
usingnamespacestd;
intmain()
{
ifstreamin("test.txt");
charstr[1024]={0};

if(in.fail())
{
cout<<"openfileerror"<<endl;
return-1;
}
while(in.getline(str,sizeof(str),' '))
{
cout<<str<<endl;
}
in.close();
return0;
}

C語言版本

#include<stdio.h>
intmain()
{
FILE*fp;
fp=fopen("test.txt","r");
charstr[1024];
if(fp==NULL)
{
printf("openfileerror ");
return-1;
}
while(fgets(str,sizeof(str),fp))
{
printf("%s",str);
}
fclose(fp);
return0;
}

H. 怎樣用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("無法打開文件讀取。 ");

}

I. 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;
}