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

c語言讀取txt

發布時間: 2022-01-16 04:29:34

c語言讀取txt

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_LINE 1024

int main()

{
char buf[MAX_LINE];

int len;

if((fp = fopen("test.txt","r")) == NULL)

{

perror("fail to read");

exit (1) ;

}
while(fgets(buf,MAX_LINE,fp) != NULL)

{
len = strlen(buf);
buf[len-1] = '\0';

printf("%s %d \n",buf,len - 1);

}

return 0;

}

⑵ c語言讀取文本文件

1、C語言標准庫提供了一系列文件操作函數。文件操作函數一般以f+單詞的形式來命名(f是file的簡寫),其聲明位於stdio.h頭文件當中。例如:fopen、fclose函數用於文件打開與關閉;fscanf、fgets函數用於文件讀取;fprintf、fputs函數用於文件寫入;ftell、fseek函數用於文件操作位置的獲取與設置。
2、常式:

#include<stdio.h>
inta;
charb,c[100];
intmain(){
FILE*fp1=fopen("input.txt","r");//打開輸入文件
FILE*fp2=fopen("output.txt","w");//打開輸出文件
if(fp1==NULL||fp2==NULL){//若打開文件失敗則退出
puts("不能打開文件!");
rturn0;
}
fscanf(fp1,"%d",&a);//從輸入文件讀取一個整數
b=fgetc(fp1);//從輸入文件讀取一個字元
fgets(c,100,fp1);//從輸入文件讀取一行字元串

printf("%ld",ftell(fp1));//輸出fp1指針當前位置相對於文件首的偏移位元組數

fputs(c,fp2);//向輸出文件寫入一行字元串
fputc(b,fp2);//向輸出文件寫入一個字元
fprintf(fp2,"%d",a);//向輸出文件寫入一個整數

fclose(fp1);//關閉輸入文件
fclose(fp2);//關閉輸出文件,相當於保存
return0;
}

⑶ C語言 讀取TXT文檔

應該是m_strFileName不支持中文字元造成的,你試試將文件名改為英文的,再賦值給m_strFileName試試。

⑷ 請問如何用c語言從txt文件中讀取數據

#include<stdio.h>
main()
{
int i=0,j=0;
int a[100];
FILE *fp;
if((fp=fopen("1.txt","rt"))==NULL)
{
printf("error!\n");
getch();
exit(1);
}
while(!feof(fp))
{fscanf(fp,"%d",&a[i]);i++;}
for(j=0;j<i;j++)
printf("%d",a[j]);
fclose(fp);
}
回答者: hwuaxj - 千總 四級 12-23 12:35
//其中的in.txt就是你要讀取數據的文件,當然把它和程序放在同一目錄
-------------------------------------

#include <stdio.h>
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語言讀取一個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("無法打開文件讀取。 ");

}

⑹ c語言 如何讀取txt內容到數組

#include<stdio.h>

#include<stdlib.h>

#define N 10000

int main()

{

FILE *fp;

if((fp=fopen("D:\123.txt","r"))==NULL)//判斷文件是否打開成功

{//讀取D盤下,名為123的文本文件

printf("文件打開失敗 ");

exit(0);

}

else

printf("文件打開成功 ");

double a[N];

for(int i=0;i<5;i++)//讀取五個浮點型數據

fscanf(fp,"%lf,",&a[i]);//fscanf函數固定格式讀取文本中的數據;

for(int i=0;i<5;i++)

printf("%.6lf, ",a[i]);

fclose(fp);

}

⑺ C語言怎麼從TXT文件中讀入數據

像這種情況,有多種方法,最簡單的方法是:

1.用"記事本"創建a.txt文件,保存在一個文件夾中,設保存在d:之下

2.編寫程序,並運行

#include<stdio.h>

intmain()

{

inta[10][4];/*假定不超過10行,每行一定有4個元素*/

inti,j;

FILE*fp;

/*打開文件*/

fp=fopen("d:\a.txt","r");/*假設a.txt在d盤根目錄下*/

if(!fp)exit(0);

for(j=0;j<4;j++)/*假定有j行*/

for(i=0;i<4;i++)

fscanf(fp,"%d",&a[j][i]);/*讀一個數據*/

/*關閉文件*/

fclose(fp);

/*顯示運行結果*/

for(j=0;j<4;j++)/*假定有j行*/

{for(i=0;i<4;i++)

printf("%4d",a[j][i]);

printf(" ");

}

return0;

}

以上演示了文本文件的讀寫操作,供你參考.

在讀寫文件時,文件內部有一個"指針"會悄悄地變化(但你看不到),所以讀一個數據後,再讀可以得到下一個數據.

⑻ 在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);
}
}

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

}

(10)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 );

}

熱點內容
移門製作下料尺寸演算法 發布:2024-04-27 03:15:02 瀏覽:641
c語言5常量 發布:2024-04-27 02:38:49 瀏覽:991
源碼怎麼搭建 發布:2024-04-27 02:33:44 瀏覽:97
java獲取參數 發布:2024-04-27 02:22:21 瀏覽:501
unixlinuxwindows 發布:2024-04-27 02:10:55 瀏覽:445
nginx禁止ip訪問網站 發布:2024-04-27 02:05:43 瀏覽:845
webrtc伺服器搭建哪家價格低 發布:2024-04-27 01:30:08 瀏覽:141
oracle資料庫無法啟動 發布:2024-04-27 01:29:20 瀏覽:613
倪萍超級訪問 發布:2024-04-27 01:23:29 瀏覽:705
java集合循環 發布:2024-04-27 01:17:18 瀏覽:593