当前位置:首页 » 编程语言 » 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);
}
}
热点内容
华硕访问点 发布:2025-02-06 15:56:57 浏览:330
excel拼接sql 发布:2025-02-06 15:50:10 浏览:500
加密手机直播 发布:2025-02-06 15:49:31 浏览:534
自带ftp服务器好用吗 发布:2025-02-06 15:26:11 浏览:109
win7访问xp局域网 发布:2025-02-06 15:17:07 浏览:524
均线差算法 发布:2025-02-06 15:13:22 浏览:459
androidbrowser 发布:2025-02-06 15:09:49 浏览:622
勇敢的心ftp 发布:2025-02-06 15:09:03 浏览:327
php日志分析 发布:2025-02-06 15:08:19 浏览:874
36脚本大厅作者 发布:2025-02-06 14:55:53 浏览:409