1通過fopen函數(shù)打開文本,例如file
創(chuàng)新互聯(lián)公司是一家朝氣蓬勃的網(wǎng)站建設(shè)公司。公司專注于為企業(yè)提供信息化建設(shè)解決方案。從事網(wǎng)站開發(fā),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),網(wǎng)站模板,微信公眾號(hào)開發(fā),軟件開發(fā),小程序制作,10余年建站對成都輕質(zhì)隔墻板等多個(gè)方面,擁有多年的網(wǎng)站制作經(jīng)驗(yàn)。
*fp=fopen("in.txt","r");//返回一個(gè)file類型的句柄
2然后就可以通過fcanf()函數(shù)對txt文本進(jìn)行讀取
3操作完文本之后用fclose()函數(shù)
關(guān)閉已經(jīng)打開的文件。
#include
int main()
{
int data;
file *fp=fopen("in.txt","r");
if(!fp)
{
printf("can't open file\n");
return -1;
}
while(!feof(fp))
{
fscanf(fp,"%d",data);
printf("%4d",data);
}
printf("\n");
fclose(fp);
return 0;
}
C語言可以使用fopen()函數(shù)讀取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\n" );
else
printf( "The file 'data' was opened\n" );
/* Open for write */
if( (stream2 = fopen( "data2", "w+" )) == NULL )
printf( "The file 'data2' was not opened\n" );
else
printf( "The file 'data2' was opened\n" );
/* Close stream */
if(fclose( stream2 ))
printf( "The file 'data2' was not closed\n" );
/* All other files are closed: */
numclosed = _fcloseall( );
printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}
擴(kuò)展資料
使用fgetc函數(shù)
#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] = '\0';
printf( "%s\n", buffer );
fclose( stream );
}
寫幾段代碼就是了
char s[100];
FILE *fp;
fp=fopen("d:\\1.txt","r");//打開文件,注意是兩個(gè)\\
gets(s,50,fp);從文件中讀取49個(gè)字符到s中
名稱欄目:關(guān)于c語言中用于txt的函數(shù)的信息
地址分享:http://vcdvsql.cn/article34/hepcpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站改版、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)