導航:首頁 > 文件教程 > c讀入txt文件

c讀入txt文件

發布時間:2024-12-02 03:16:12

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

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

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

}

(2)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文件內容

用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文件。

1、首先打開編輯的頁面中,引入需要的文件,輸入代碼

#include <stdio.h>

#include <stdlib.h>

Ⅳ C語言 文件操作,要讀取一個txt文件內容

//data.txt文件內容如下

1 個 豬內
2 個 豬
3 個 豬
4 個 豬
5 個 豬
6 個 豬
7 個 豬
8 個 豬

//運行結果容一
the 8 line :8 個 豬

Press any key to continue
//運行結果二
out of range!
Press any key to continue

//代碼如下
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main(void)
{
int lid,cnt=0,flag=0;;
char buf[100]="\0";
FILE *fp;

srand((unsigned)time(NULL));
fp=fopen("data.txt","r");
lid= rand()%10+1;
while (fgets(buf,99,fp)!=NULL)
{
if(cnt==lid)
{
printf("the %d line :%s\n",lid+1,buf);
flag=1;
break;
}
cnt++;
}
if (flag==0)
{
printf("out of range!\n");
}
}

Ⅵ C語言如何讀取TXT文件並存入數組中

一、編程思路。
1 以文本方式打開文件。
2 循環用fscanf格式化輸入數據到數組。
3 判斷fscanf的返回值,如果顯示到達文件結尾,退出輸入。
4 關閉文件。
5 使用數據。

二、代碼實現。
設定文件名為in.txt, 存有一系列整型數據,以空格或換行分隔。
代碼可以寫作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <stdio.h>
int main()
{
int v[100];//開一個足夠大的數組。
int i = 0, j;
FILE *fp;//文件指針
fp = fopen("in.txt", "r");//以文本方式打開文件。
if(fp == NULL) //打開文件出錯。
return -1;
while(fscanf(fp, "%d", &v[i]) != EOF) //讀取數據到數組,直到文件結尾(返回EOF)
i++;
fclose(fp);//關閉文件
for(j = 0; j < i; j ++)//循環輸出數組元素。
{
printf("%d ", v[j]);
}
return 0;
}

當文件內容為:
1 35 6 8 9 9
10 123 34
76 54 98
程序輸出:
1 35 6 8 9 9 10 123 34 76 54 98

閱讀全文

與c讀入txt文件相關的資料

熱點內容
maya粒子表達式教程 瀏覽:84
抖音小視頻如何掛app 瀏覽:283
cad怎麼設置替補文件 瀏覽:790
win10啟動文件是空的 瀏覽:397
jk網站有哪些 瀏覽:134
學編程和3d哪個更好 瀏覽:932
win10移動硬碟文件無法打開 瀏覽:385
文件名是亂碼還刪不掉 瀏覽:643
蘋果鍵盤怎麼打開任務管理器 瀏覽:437
手機桌面文件名字大全 瀏覽:334
tplink默認無線密碼是多少 瀏覽:33
ipaddgm文件 瀏覽:99
lua語言編程用哪個平台 瀏覽:272
政采雲如何導出pdf投標文件 瀏覽:529
php獲取postjson數據 瀏覽:551
javatimetask 瀏覽:16
編程的話要什麼證件 瀏覽:94
錢脈通微信多開 瀏覽:878
中學生學編程哪個培訓機構好 瀏覽:852
榮耀路由TV設置文件共享錯誤 瀏覽:525

友情鏈接