關於c語言讀寫大型txt文件的信息

本文目錄一覽:

怎樣用C語言寫入\讀取一個TXT文件

如果預知前面的是英文後面的是中文,即可分開:

#includestdio.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;in;i++ ) printf(“%s “,y[i]); printf(“\n”);

  printf(“中文: “); for ( i=0;in;i++ ) printf(“%s “,h[i]); printf(“\n”);

} else printf(“無法打開文件讀取。\n”);

}

如果中英文順序不一定,且不會有中英文混合單詞:

#includestdio.h

#includestring.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;in;i++ ) printf(“%s “,y[i]); printf(“\n”);

  printf(“中文: “); for ( i=0;in;i++ ) printf(“%s “,h[i]); printf(“\n”);

} else printf(“無法打開文件讀取。\n”);

}

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

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\n” );

else

printf( “The file ‘data’ was opened\n” );

/* Open for write */

if( (stream2 = fopen( “data2”, “w+” )) == NULL )

printf( “The file ‘data2’ was not opened\n” );

else

printf( “The file ‘data2’ was opened\n” );

/* Close stream */

if(fclose( stream2 ))

printf( “The file ‘data2’ was not closed\n” );

/* All other files are closed: */

numclosed = _fcloseall( );

printf( “Number of files closed by _fcloseall: %u\n”, numclosed );

}

擴展資料

使用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] = ‘\0’;

printf( “%s\n”, buffer );

fclose( stream );

}

C語言如何實現對txt文件的讀取和寫入

使用fopen的r方式可以實現讀取,用w+方式可以實現寫入。

1.fopen的函數原型:FILE

*

fopen(const

char

*

path,const

char

*

mode);

fopen函數的第一個參數是文件路徑,第二個參數是打開方式,有以下幾種方式:

r

以只讀方式打開文件,該文件必須存在。

r+

以可讀寫方式打開文件,該文件必須存在。

rb+

讀寫打開一個二進制文件,允許讀數據。

rw+

讀寫打開一個文本文件,允許讀和寫。

w

打開只寫文件,若文件存在則文件長度清為0,即該文件內容會消失。若文件不存在則建立該文件。

w+

打開可讀寫文件,若文件存在則文件長度清為零,即該文件內容會消失。若文件不存在則建立該文件。

a

以附加的方式打開只寫文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數據會被加到文件尾,即文件原先的內容會被保留。(EOF符保留)

a+

以附加方式打開可讀寫的文件。若文件不存在,則會建立該文件,如果文件存在,寫入的數據會被加到文件尾後,即文件原先的內容會被保留。

(原來的EOF符不保留)

wb

只寫打開或新建一個二進制文件;只允許寫數據。

wb+

讀寫打開或建立一個二進制文件,允許讀和寫。

wt+

讀寫打開或着建立一個文本文件;允許讀寫。

at+

讀寫打開一個文本文件,允許讀或在文本末追加數據。

ab+

讀寫打開一個二進制文件,允許讀或在文件末追加數據。

上述的形態字符串都可以再加一個b字符,如rb、w+b或ab+等組合,加入b

字符用來告訴函數庫打開的文件為二進制文件,而非純文字文件。

返回值:文件順利打開後,指向該流的文件指針就會被返回。如果文件打開失敗則返回NULL,並把錯誤代碼存在errno中。

2.例程:

#includestdio.h

#define F_PATH “d:\\myfile\\file.dat”

char c;

int main(){

FILE*fp=NULL;//需要注意

fp=fopen(F_PATH,”w”); //創建文件

if(NULL==fp) return -1;//要返回錯誤代碼

while(scanf(“%c”,c)!=EOF) fprintf(fp,”%c”,c); //從控制台中讀入並在文本輸出

fclose(fp);

fp=NULL;//需要指向空,否則會指向原打開文件地址

return 0;

}

c語言如何讀寫大型的txt文件

#include stdio.h

#include string.h

#define MAXSIZE 4000000

struct password {

char psw[12]; // 密碼名稱

int counter;  // 出現次數計數器

};

int Append(struct password a[], int *n, char psw[]) {

int i;

for(i = 0; i  *n; ++i) {

if(strcmp(a[i].psw,psw) == 0) {

++a[i].counter;

return 2;

}

}

if(*n  MAXSIZE) {

strcpy(a[*n].psw,psw);

a[*n].counter = 1;

++(*n);

return 1;

}

return 0;

}

 

int main() {

struct password a[MAXSIZE];

char psw[12];

    int i,n = 0,id;

char infilename[] = “indata.txt”;

char outfilename[] = “outdata.txt”;

FILE *inf,*outf;

if((inf = fopen(infilename,”rt”)) == NULL) {

printf(“不能打開數據文件:%s。\n”,infilename);

return 1;

}

    while(fscanf(inf,”%d %11s”,id,psw) == 2) {

if(Append(a,n,psw) == 0) break;

}

fclose(inf);

if((outf = fopen(outfilename,”wt”)) == NULL) {

printf(“不能打開數據文件:%s。\n”,outfilename);

return 2;

}

for(i = 0; i  n; ++i)

fprintf(outf,”%s %d\n”,a[i].psw,a[i].counter);

fclose(outf);

return 0;

}

C語言 txt文件的讀寫方法?

例子一個,比較簡單:

#includestdio.h

#includestring.h

void main()

{

char line[50];

char name[20], ps[8];

FILE *fp = fopen(“User.txt”,”r”);

if(!fp)

{

return;

}

while(!feof(fp))

{

memset(line,0,50);

memset(name,0,20);

memset(ps,0,8);

fgets(line,50,fp);

sscanf(line, “%s%s”, name, ps);

printf(“Name:[%s], Pass:[%s]\n”,name,ps);

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/196906.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-03 13:26
下一篇 2024-12-03 13:27

相關推薦

發表回復

登錄後才能評論