導航:首頁 > 文件教程 > 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文件相關的資料

熱點內容
cefs文件系統 瀏覽:404
學平面設計個編程哪個好 瀏覽:701
如何把編程文件轉為hex 瀏覽:80
清除蘋果地圖來自地址 瀏覽:233
已經打開的文件如何清理 瀏覽:685
視頻網站有什麼用 瀏覽:70
多個表格文件怎樣壓縮文件 瀏覽:729
cad文件大很卡如何解決 瀏覽:633
將java程序打包成apk 瀏覽:277
2021唱吧文件找不到了 瀏覽:463
華為p9手機文件管理 瀏覽:284
固定在工具欄的文件夾怎麼查找路徑 瀏覽:667
半條命2有幾個版本 瀏覽:333
電腦管家微信備份文件夾 瀏覽:826
ubuntu文件夾解鎖 瀏覽:34
網站多少錢一單 瀏覽:382
系統鏡像恢復找不到文件 瀏覽:255
進什麼網站 瀏覽:588
修改了配置文件代碼沒有讀出來 瀏覽:749
vss資料庫是什麼 瀏覽:899

友情鏈接