c語言讀取txt數據
『壹』 用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文件的讀取和寫入
1、使用VS新建空工程,直接點擊確定,如下所示。
『叄』 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文件中的信息
一般來說在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文檔
應該是m_strFileName不支持中文字元造成的,你試試將文件名改為英文的,再賦值給m_strFileName試試。
『陸』 c語言讀取txt文件裡面的數據
在C語言中,文件操作都是由庫函數來完成的。
要讀取一個txt文件,首先要使用文件打開函數fopen()。
fopen函數用來打開一個文件,其調用的一般形式為: 文件指針名=fopen(文件名,使用文件方式) 其中,「文件指針名」必須是被說明為FILE 類型的指針變數,「文件名」是被打開文件的文件名。 「使用文件方式」是指文件的類型和操作要求。「文件名」是字元串常量或字元串數組。
其次,使用文件讀寫函數讀取文件。
在C語言中提供了多種文件讀寫的函數:
·字元讀寫函數 :fgetc和fputc
·字元串讀寫函數:fgets和fputs
·數據塊讀寫函數:freed和fwrite
·格式化讀寫函數:fscanf和fprinf
最後,在文件讀取結束要使用文件關閉函數fclose()關閉文件。
下面以格式化讀寫函數fscanf和fprintf為例,實現對文件A.txt(各項信息以空格分割)的讀取,並將它的信息以新的格式(用製表符分割各項信息)寫入B.txt,實現對A.txt的處理。
C語言源程序如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct student{
char name[32];
int no;
char sex[16];
float score;
} stu;
int main(int argc, char* argv[])
{
//打開文件
FILE * r=fopen("A.txt","r");
assert(r!=NULL);
FILE * w=fopen("B.txt","w");
assert(w!=NULL);
//讀寫文件
stu a[128];
int i=0;
while(fscanf(r,"%s%d%s%f",a[i].name,&a[i].no,a[i].sex,&a[i].score)!=EOF)
{
printf("%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//輸出到顯示器屏幕
fprintf(w,"%s\t%d\t%s\t%g\n",a[i].name,a[i].no,a[i].sex,a[i].score);//輸出到文件B.txt
i++;
}
//關閉文件
fclose(r);
fclose(w);
system("pause");
return 0;
}
『柒』 如何用c語言從txt文件中讀取數據
//其中的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文本裡面的內容
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 );
}
(8)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] = '