導航:首頁 > 編程大全 > 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資料庫相關的資料

熱點內容
手機里excel文件導出 瀏覽:577
holo手機文件管理器 瀏覽:198
XLS1個文件夾能存多少kB 瀏覽:703
revit2014建築設計教程 瀏覽:723
如何把圖片放到word文件中去 瀏覽:704
開通微信原創是不是每一篇文章 瀏覽:528
編程在大學什麼專業學的好 瀏覽:692
學生文件櫃多少錢 瀏覽:229
文件夾進入cmd 瀏覽:357
數據線編程軟體哪個最好 瀏覽:734
你說什麼美女做什麼的app 瀏覽:596
jsp實現資料庫管理系統 瀏覽:968
電腦word文件怎麼弄到桌面 瀏覽:425
ps文件夾解鎖方法 瀏覽:863
phpsocket多個連接資料庫 瀏覽:87
現在不玩微博了都在玩什麼app 瀏覽:629
linux查看文件夾目錄 瀏覽:955
為什麼源文件 瀏覽:158
解壓了文件用什麼打開 瀏覽:675
怎麼給軟體創建文件夾 瀏覽:589

友情鏈接