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

熱點內容
火車票時刻表及票價資料庫 瀏覽:94
求平均值java 瀏覽:767
linux如何分屏顯示 瀏覽:257
手機可以直接填寫pdf文件么 瀏覽:554
linux虛擬光碟機軟體 瀏覽:836
米2s最好的版本 瀏覽:640
小米6檢測工具下載 瀏覽:540
桌面創建不了文件夾怎麼回事 瀏覽:894
qq古代頭像傷感圖片 瀏覽:797
wpspdf文檔如何在文件夾預覽 瀏覽:15
大數據與工業的結合包括哪些領域 瀏覽:993
蘋果7鬧鍾鈴聲設置歌曲 瀏覽:168
法庭文件有哪些 瀏覽:211
zip文件怎麼刪除嗎 瀏覽:247
win1016179安裝失敗 瀏覽:457
pp助手的文件系統 瀏覽:218
設計網站橫幅廣告要哪些因素 瀏覽:991
勞務報酬認定在多少號文件 瀏覽:209
本地數據傳輸業務有什麼用途 瀏覽:375
什麼情侶app好用 瀏覽:575

友情鏈接