为了账号安全,请及时绑定邮箱和手机立即绑定

将以下两个函数通过一个主函数调用,求调用后的程序

将以下两个函数通过一个主函数调用,求调用后的程序

C PHP
斯蒂芬大帝 2023-03-05 18:14:31
文件的分割#include<stdio.h>int main(){int len=0;int len2=0;FILE* stream;FILE* stream1;FILE* stream2;char buf[50];char buf1[50];char buf2[50];char text[1024];printf("input anfile path to open:");scanf("%s",buf);stream=fopen(buf,"r+");fseek(stream,0,SEEK_END);len=ftell(stream);printf("the file %s length is %d!\n",buf,len);len2 = len/2;printf("intput 2 file name: \n");scanf("%s%s",buf1,buf2);fseek(stream,0,SEEK_SET);stream1=fopen(buf1,"w+");stream2=fopen(buf2,"w+");fread(text,len2,1,stream);fwrite(text,len2,1,stream1);fread(text,len-len2,1,stream);fwrite(text,len-len2,1,stream2);fclose(stream);fclose(stream1);fclose(stream2);return 0;} 文件的合并#include<stdio.h>int main(){int len=0;int len2=0;FILE* stream;FILE* stream1;char buf[50];char buf1[50];char text[1024];printf("input anfile path to open:");scanf("%s",buf);stream=fopen(buf,"r+");fseek(stream,0,SEEK_END);printf("intput another file name: \n");scanf("%s",buf1);stream1=fopen(buf1,"r+");fseek(stream1,0,SEEK_END);len=ftell(stream1);fseek(stream1,0,SEEK_SET);fread(text,len,1,stream1);fwrite(text,len,1,stream);fclose(stream);fclose(stream1);remove(buf1);//remove the another filereturn 0;}
查看完整描述

2 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

首先,c中有且仅有一个主函数,所以第一步,你必须的把两个函数改成其他名字:
#include <stdio.h>

int function1()
{
int len=0;
int len2=0;
FILE* stream;
FILE* stream1;
FILE* stream2;
char buf[50];
char buf1[50];
char buf2[50];
char text[1024];

printf("input anfile path to open:");

scanf("%s",buf);
stream=fopen(buf,"r+");

fseek(stream,0,SEEK_END);
len=ftell(stream);
printf("the file %s length is %d!\n",buf,len);
len2 = len/2;

printf("intput 2 file name: \n");
scanf("%s%s",buf1,buf2);
fseek(stream,0,SEEK_SET);
stream1=fopen(buf1,"w+");
stream2=fopen(buf2,"w+");
fread(text,len2,1,stream);
fwrite(text,len2,1,stream1);
fread(text,len-len2,1,stream);
fwrite(text,len-len2,1,stream2);

fclose(stream);
fclose(stream1);
fclose(stream2);

return 0;

int function2()
{
int len=0;
int len2=0;
FILE* stream;
FILE* stream1;

char buf[50];
char buf1[50];

char text[1024];

printf("input anfile path to open:");

scanf("%s",buf);
stream=fopen(buf,"r+");

fseek(stream,0,SEEK_END);

printf("intput another file name: \n");
scanf("%s",buf1);

stream1=fopen(buf1,"r+");
fseek(stream1,0,SEEK_END);
len=ftell(stream1);
fseek(stream1,0,SEEK_SET);
fread(text,len,1,stream1);
fwrite(text,len,1,stream);

fclose(stream);
fclose(stream1);
remove(buf1);//remove the another file

return 0;
}

//第二部:如果有条件的调用它们的话,加上if语句或者switch语句,基本上都是这样,此外,你可以把function1 和function2放入一个头文件中,然后包含这个头文件亦行。
void main()
{

function1();
function2();
}

 


查看完整回答
反对 回复 2023-03-08
?
倚天杖

TA贡献1828条经验 获得超3个赞

不知道你的具体要求是什么,如果想从主函数调用这两个函数,把这两个函数名改完就可以了
#include<stdio.h>
//文件的分裂
int SplitFile ()//修改的地方
{
。。。。。 } 
文件的合并
#include<stdio.h>

int ConveFile()//修改的地方
{。。。
}
void main()
{
//如果是有条件的选择这两个函数的话,也可以加一些if语句
SplitFile();
ConveFile();
}


查看完整回答
反对 回复 2023-03-08
  • 2 回答
  • 0 关注
  • 66 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信