C语言,如何用循环读入多个文本文件
推荐于2018-04-12
展开全部
下面的代码是刚刚给你写好的,文件名仅限"a.txt" "b.txt"和"c.txt"#include"stdio.h"
#include"stdlib.h"
int main(void)
{
FILE *fp;
char filename[]="a.txt";
int i;
void *p[3];
long filelen;
for(i=0;i<3;i++)
{
fp=fopen(filename,"rb");
if(!fp){printf("\"%s\"文件打开失败\n",filename);continue;}
fseek(fp,0,2);
filelen=ftell(fp);
fseek(fp,0,0);
p[i]=malloc((filelen+1)*sizeof(char));
fread(p[i],filelen*sizeof(char),1,fp);
*((char *)p[i]+filelen)='\0';
fclose(fp);
filename[0]++;
}
for(i=0;i<3;i++)puts(p[i]);
return 0;
}
#include"stdlib.h"
int main(void)
{
FILE *fp;
char filename[]="a.txt";
int i;
void *p[3];
long filelen;
for(i=0;i<3;i++)
{
fp=fopen(filename,"rb");
if(!fp){printf("\"%s\"文件打开失败\n",filename);continue;}
fseek(fp,0,2);
filelen=ftell(fp);
fseek(fp,0,0);
p[i]=malloc((filelen+1)*sizeof(char));
fread(p[i],filelen*sizeof(char),1,fp);
*((char *)p[i]+filelen)='\0';
fclose(fp);
filename[0]++;
}
for(i=0;i<3;i++)puts(p[i]);
return 0;
}
2013-09-13
展开全部
FILE* file;char temp[ 3 ][ 256 ] ={�0�2 �0�2 "a.txt",�0�2 �0�2 "b.txt",�0�2 �0�2 "c.txt"};char data[ 3 ][ 1024 ];for ( int i = 0; i < 3; ++i ){�0�2 �0�2 file = fopen( temp[ i ], "r" );�0�2 �0�2 if ( file == 0 )�0�2 �0�2 �0�2 �0�2continue;�0�2 �0�2 fread( data[ i ], 1024, 1, file );�0�2 �0�2 fclose( file );}数组可以动态分配!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
下面的代码是刚刚给你写好的,文件名仅限"a.txt"
"b.txt"和"c.txt"#include"stdio.h"
#include"stdlib.h"
int
main(void)
{
FILE
*fp;
char
filename[]="a.txt";
int
i;
void
*p[3];
long
filelen;
for(i=0;i<3;i++)
{
fp=fopen(filename,"rb");
if(!fp){printf("\"%s\"文件打开失败\n",filename);continue;}
fseek(fp,0,2);
filelen=ftell(fp);
fseek(fp,0,0);
p[i]=malloc((filelen+1)*sizeof(char));
fread(p[i],filelen*sizeof(char),1,fp);
*((char
*)p[i]+filelen)='\0';
fclose(fp);
filename[0]++;
}
for(i=0;i<3;i++)puts(p[i]);
return
0;
}
"b.txt"和"c.txt"#include"stdio.h"
#include"stdlib.h"
int
main(void)
{
FILE
*fp;
char
filename[]="a.txt";
int
i;
void
*p[3];
long
filelen;
for(i=0;i<3;i++)
{
fp=fopen(filename,"rb");
if(!fp){printf("\"%s\"文件打开失败\n",filename);continue;}
fseek(fp,0,2);
filelen=ftell(fp);
fseek(fp,0,0);
p[i]=malloc((filelen+1)*sizeof(char));
fread(p[i],filelen*sizeof(char),1,fp);
*((char
*)p[i]+filelen)='\0';
fclose(fp);
filename[0]++;
}
for(i=0;i<3;i++)puts(p[i]);
return
0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询