當前位置:首頁 » 編程語言 » c語言從txt讀取數據

c語言從txt讀取數據

發布時間: 2023-08-07 20:08:56

A. c語言如何實現對txt文件的讀取和寫入

1、使用VS新建空工程,直接點擊確定,如下所示。

B. 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. 怎樣用C語言寫入\讀取一個TXT文件

如果預知前面的是英文後面的是中文,即可分開:

#include<stdio.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]); n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("無法打開文件讀取。 ");

}

如果中英文順序不一定,且不會有中英文混合單詞:

#include<stdio.h>

#include<string.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]);

if ( y[n][0]<0 ) { strcpy(s,y[n]);strcpy(y[n],h[n]);strcpy(h[n],s); } //漢字字元ASCII碼小於0

n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("無法打開文件讀取。 ");

}

D. C語言文件操作,要讀取一個txt文件內容,應該怎麼做

//data.txt文件內容如下x0dx0ax0dx0a1個豬x0dx0a2個豬x0dx0a3個豬x0dx0a4個豬x0dx0a5個豬x0dx0a6個豬x0dx0a7個豬x0dx0a8個豬x0dx0ax0dx0a//運行結果一x0dx0athe 8 line :8 個 豬x0dx0ax0dx0aPress any key to continue x0dx0a//運行結果二x0dx0aout of range!x0dx0aPress any key to continue x0dx0ax0dx0a//代碼如下x0dx0a#include x0dx0a#include x0dx0a#include x0dx0amain(void)x0dx0a{x0dx0aint lid,cnt=0,flag=0;;x0dx0achar buf[100]="\0";x0dx0aFILE *fp;x0dx0ax0dx0asrand((unsigned)time(NULL));x0dx0afp=fopen("data.txt","r");x0dx0alid= rand()%10+1;x0dx0awhile (fgets(buf,99,fp)!=NULL)x0dx0a{x0dx0aif(cnt==lid)x0dx0a{x0dx0aprintf("the %d line :%s\n",lid+1,buf);x0dx0aflag=1;x0dx0abreak;x0dx0a}x0dx0acnt++;x0dx0a}x0dx0aif (flag==0)x0dx0a{x0dx0aprintf("out of range!\n");x0dx0a}x0dx0a}

E. 怎樣用C語言從txt文件中讀入數據

1 以fopen打開文件,使用"r"方式。

2 通過fscanf,按照文件中的數據格式,讀入數據。

3 關閉文件並使用數據。

如文件in.txt中存在三個以空格分隔的數據,依次為整型,字元串,以及浮點型,則讀取數據的代碼可以寫作:

intmain()
{
FILE*fp;
inta;
chars[100];
floatf;
fp=fopen("in.txt","r");
if(fp==NULL)return-1;//打開文件失敗,結束程序。
fscanf(fp,"%d%s%f",&a,s,&f);
fclose(fp);

printf("readvalue:%d,%s,%f",a,s,f);
}

F. 用c語言讀取一個txt文件

如果預知前面的是英文後面的是中文,即可分開:

#include<stdio.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]); n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("無法打開文件讀取。 ");

}

如果中英文順序不一定,且不會有中英文混合單詞:

#include<stdio.h>

#include<string.h>

#define N 100

void main() { FILE *fp; char s[256],y[N][20],h[N][20]; int i,n;

if ( fp=fopen("c:\data\text.txt","r") ) {

n=0;

while ( !feof(fp) ) {

fgets(s,256,fp); sscanf("%s%s",y[n],h[n]);

if ( y[n][0]<0 ) { strcpy(s,y[n]);strcpy(y[n],h[n]);strcpy(h[n],s); } //漢字字元ASCII碼小於0

n++; if ( n>=N ) break;

}

fclose(fp);

printf("英文: "); for ( i=0;i<n;i++ ) printf("%s ",y[i]); printf(" ");

printf("中文: "); for ( i=0;i<n;i++ ) printf("%s ",h[i]); printf(" ");

} else printf("無法打開文件讀取。 ");

}

G. 在c語言中,如何讀取一個txt文件中的信息

一般來說在C語言中讀取txt文件的信息有兩種方法,一種是使用C語言標准文件I/O中的fopen()、fread()等等函數,一種是調用操作系統中的API函數,比如Windows上的ReadFile()、OpenFile()等等,現在操作系統一般都具備內存文件映射功能,對於大的txt文件,一般都使用這種方式操作。下面是一個使用C語言標准文件I/O操作文件的例子。

#include<stdio.h>

FILE*stream;

voidmain(void)
{
longl;
floatfp;
chars[81];
charc;

stream=fopen("fscanf.out","w+");
if(stream==NULL)
printf("Thefilefscanf.outwasnotopened ");
else
{
fprintf(stream,"%s%ld%f%c","helloworld",
65000,3.14159,'x');

/*Setpointertobeginningoffile:*/
fseek(stream,0L,SEEK_SET);

/*Readdatabackfromfile:*/
fscanf(stream,"%s",s);
fscanf(stream,"%ld",&l);

fscanf(stream,"%f",&fp);
fscanf(stream,"%c",&c);

/*Outputdataread:*/
printf("%s ",s);
printf("%ld ",l);
printf("%f ",fp);
printf("%c ",c);

fclose(stream);
}
}
熱點內容
unzipforlinux 發布:2025-03-21 08:49:57 瀏覽:952
安卓wlan信號慢怎麼辦 發布:2025-03-21 08:35:35 瀏覽:869
keil無法編譯 發布:2025-03-21 08:25:51 瀏覽:308
怎麼設置蘋果鎖屏密碼忘了怎麼辦啊 發布:2025-03-21 08:20:48 瀏覽:621
報稅自然人密碼是什麼 發布:2025-03-21 08:20:47 瀏覽:480
我的世界開局送一套房的伺服器 發布:2025-03-21 08:16:32 瀏覽:66
網格搜索演算法 發布:2025-03-21 08:16:29 瀏覽:516
君越哪個配置有液晶儀表盤 發布:2025-03-21 08:10:44 瀏覽:873
mysql資料庫名字 發布:2025-03-21 08:10:04 瀏覽:366
安卓如何掃描瀏覽器中的二維碼 發布:2025-03-21 08:04:10 瀏覽:678