導航:首頁 > 編程大全 > c語言讀不出來txt資料庫

c語言讀不出來txt資料庫

發布時間:2023-01-10 22:19:30

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

}

② C語言文件讀取時讀不到文件

1、使用fopen、fread、fwrite等C語言標准庫中的文件I/O函數即可對文件進行打開、讀取、寫入操作。下面的程序創建一個.bin的文件,然後再從裡面讀取數據。
#include
<stdio.h>
int
main()
{
FILE*
pFile;
float
buffer[]
=
{
2.0
,
3.0
,
8.0
};
pFile
=
fopen("myfile.bin"
,
"wb");
//
打開文件寫操作
fwrite(buffer
,
1
,
sizeof(buffer)
,
pFile);
//
把浮點數組寫到文件
myfile.bin
fclose(pFile);
//
關閉文件
float
read[3];
pFile
=
fopen("myfile.bin"
,
"rb");
//
重新打開文件讀操作
fread(read
,
1
,
sizeof(read)
,
pFile);
//
從文件中讀數據
printf("%f\t%f\t%f\n",
read[0],
read[1],
read[2]);
fclose(pFile);
//
關閉文件
return
0;
}
2、bin文件一般是指的二進制文件,它往往具有兩種形式,一種是某種系統平台的可執行文件格式,比如windows平台的PE文件格式,Linux平台的ELF文件格式,另外一種是提取的一段二進制代碼,比如系統的主引導記錄mbr,就是一個包含512個位元組的程序,用c語言對這類文件進行讀取時,必須按一定格式進行解析。而且常用的方法不是使用C語言的I/O操作,而一般使用系統提供的文件內存映射方法。

③ 如何用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語言 讀取文件失敗

讀取文件失敗,很有可能是代碼問題,參考代碼如下,注意注釋:
#include "stdio.h"
#include <stdlib.h>
int main(int argc, char* argv[])
{
FILE *fp;
int i,t,sum=0; //sum需要初始化為版0
fp=fopen("d:\\1.txt","r"); //打開文件權
if(!fp)
{
printf("打開文件失敗\n");//確認文件打開
exit(1);
}
for(i=0;i<3;i++)
{
fscanf(fp,"%d",&t); //這里使用的應該是fscanf,而不是scanf
sum=sum+t;

}
fclose(fp);
printf("%d",sum); //這里應該這么寫,先前的printf(sum); 寫法是錯的
return 0;

閱讀全文

與c語言讀不出來txt資料庫相關的資料

熱點內容
電腦版上傳視頻找不到文件在哪 瀏覽:164
javastringtemplate 瀏覽:594
聯想啟天m7300能升級內存嗎 瀏覽:698
精雕怎麼打開圖片文件 瀏覽:161
ug編程鏜孔g76如何使用 瀏覽:719
7歲到18歲少兒編程學什麼 瀏覽:413
mac文件怎麼列印 瀏覽:756
農葯登記許可證查詢哪個網站 瀏覽:857
素材站用什麼網站系統 瀏覽:173
ug如何用鉸刀編程 瀏覽:647
三國志版本介紹 瀏覽:260
情侶用的定位app有哪些 瀏覽:244
怎麼清楚蘋果手機的健身數據 瀏覽:988
淘寶美工教程自學網 瀏覽:452
父子2006未刪減版本 瀏覽:872
包頭誰賣蘋果6 瀏覽:504
化工企業停工有哪些文件通知要求 瀏覽:878
下載軟體後文件夾在桌面 瀏覽:397
word文字編組 瀏覽:167
旅遊策劃的程序 瀏覽:519

友情鏈接