當前位置:首頁 » 編程語言 » C語言txt讀取

C語言txt讀取

發布時間: 2023-06-04 03:01:36

1. 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 );

}

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

}

2. 怎麼用C語言讀取 TXT文件中的字元串

1、首先我們打開電腦里的VS軟體,使用VS新建空工程,直接點擊確定。

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

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

1、使用VS新建空工程,直接點擊確定,如下所示。

5. C語言中讀取txt文件內容

1通過fopen函數打開文本,例如FILE *fp=fopen("in.txt","r");//返回一個FILE類型的句柄

2然後就可以通過fcanf()函數對txt文本進行讀取

3操作完文本之後用fclose()函數 關閉已經打開的文件。

#include<stdio.h>
intmain()
{
intdata;
FILE*fp=fopen("in.txt","r");
if(!fp)
{
printf("can'topenfile ");
return-1;
}
while(!feof(fp))
{
fscanf(fp,"%d",&data);
printf("%4d",data);
}
printf(" ");
fclose(fp);
return0;
}

6. 用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;
}

7. 怎樣用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);
}

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

}

9. C語言文件操作,要讀取一個txt文件內容,應該怎麼做

//data.txt文件內容如下x0dx0ax0dx0a1個豬x0dx0a2個豬x0dx0a3個豬x0dx0a4個豬x0dx0a5個豬x0dx0a6個豬x0dx0a7個豬x0dx0a8個豬x0dx0ax0dx0a//運行結果一x0dx0athe 8 line :8 個 豬x0dx0ax0dx0aPress any key to continue x0dx0a//運行結果二x0dx0aout of range!x0dx0aPress any key to continue x0dx0ax0dx0a//代碼如下x0dx0a#include x0dx0a#include x0dx0a#include x0dx0amain(void)x0dx0a{x0dx0aint lid,cnt=0,flag=0;;x0dx0achar buf[100]="\0";x0dx0aFILE *fp;x0dx0ax0dx0asrand((unsigned)time(NULL));x0dx0afp=fopen("data.txt","r");x0dx0alid= rand()%10+1;x0dx0awhile (fgets(buf,99,fp)!=NULL)x0dx0a{x0dx0aif(cnt==lid)x0dx0a{x0dx0aprintf("the %d line :%s\n",lid+1,buf);x0dx0aflag=1;x0dx0abreak;x0dx0a}x0dx0acnt++;x0dx0a}x0dx0aif (flag==0)x0dx0a{x0dx0aprintf("out of range!\n");x0dx0a}x0dx0a}

熱點內容
定義dns伺服器的ip 發布:2025-05-17 20:32:37 瀏覽:952
android判斷圖片 發布:2025-05-17 20:32:33 瀏覽:832
安卓12什麼時候適配小米 發布:2025-05-17 20:31:47 瀏覽:69
c語言字元串初始化 發布:2025-05-17 20:18:43 瀏覽:35
安卓融e聯推送需要什麼許可權 發布:2025-05-17 20:18:39 瀏覽:268
我的世界無限武魂伺服器 發布:2025-05-17 20:17:09 瀏覽:371
安卓手游腳本語言 發布:2025-05-17 19:53:07 瀏覽:21
找圈演算法 發布:2025-05-17 19:49:19 瀏覽:410
資料庫的存取方法 發布:2025-05-17 19:48:36 瀏覽:125
androidapp測試 發布:2025-05-17 19:48:19 瀏覽:389