题目:
修改下列程序,用命令行界面代替交互式界面
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
#define SLEN 81
void append(FILE * source,FILE * dest);
char * s_gets(char * st,int n);
int main(){
FILE * fa, * fs;
int files = 0;
char file_app[SLEN];
char file_src[SLEN];
int ch;
puts("Enter name of destination file: ");
s_gets(file_app,SLEN); //输入目标文件名
if((fa = fopen(file_app,"a+")) == NULL){
fprintf(stderr,"cant't open %s\n",file_app);
exit(EXIT_FAILURE);
}
if(setvbuf(fa,NULL,_IOFBF,BUFSIZE) != 0){
fputs("Can't create output buffer\n",stderr);
exit(EXIT_FAILURE);
}
puts("Enter name of first source file (empty line to quit): ");
while(s_gets(file_src,SLEN) && file_src[0] != '\0'){
if(strcmp(file_src,file_app) == 0) fputs("Can't append file to itself\n",stderr);
else if((fs = fopen(file_src,"r")) == NULL) fprintf(stderr,"Can't open %s\n",file_src);
else{
if(setvbuf(fs,NULL,_IOFBF,BUFSIZE) != 0){
fputs("Cant's append file to itself\n",stderr);
continue;
}
append(fs,fa);
if(ferror(fs) != 0) fprintf(stderr,"Error in reading file %s.\n",file_src);
if(ferror(fa) != 0) fprintf(stderr,"Error in writing file %s.\n",file_app);
fclose(fs); //关闭读取文件,继续下一次的读取
files++;
printf("File %s appended.\n",file_src);
puts("Next file (empty line to quit): ");
}
}
printf("Done appending. %d files appended.\n",files);
rewind(fa);
printf("%s contents:\n",file_app);
while((ch = getc(fa)) != EOF) putchar(ch);
puts("Done displaying.");
fclose(fa);
return 0;
}
void append(FILE * source,FILE * dest){
size_t bytes;
static char temp[BUFSIZE];
while(( bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0){
fwrite(temp,sizeof(char),bytes,dest);
}
}
char * s_gets(char * st,int n){
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val){
find = strchr(st,'\n');
if(find) *find = '\0';
else{
while(getchar() != '\n')
continue;
}
}
}
源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
#define SLEN 81
void append(FILE * source,FILE * dest);
char * s_gets(char * st,int n);
int main(int argc,char * argv[]){
FILE * fa, * fs;
int files = 0;
int ch;
if((fa = fopen(argv[1],"a+")) == NULL){
fprintf(stderr,"cant't open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fa,NULL,_IOFBF,BUFSIZE) != 0){
fputs("Can't create output buffer\n",stderr);
exit(EXIT_FAILURE);
}
for(int i = 2;i <= argc-1;i++){
if(strcmp(argv[i],argv[1]) == 0) fputs("Can't append file to itself\n",stderr);
else if((fs = fopen(argv[i],"r")) == NULL) fprintf(stderr,"Can't open %s\n",argv[i]);
else{
if(setvbuf(fs,NULL,_IOFBF,BUFSIZE) != 0){
fputs("Cant's append file to itself\n",stderr);
continue;
}
append(fs,fa);
if(ferror(fs) != 0) fprintf(stderr,"Error in reading file %s.\n",argv[i]);
if(ferror(fa) != 0) fprintf(stderr,"Error in writing file %s.\n",argv[1]);
fclose(fs); //关闭读取文件,继续下一次的读取
files++;
printf("File %s appended.\n",argv[i]);
}
}
printf("Done appending. %d files appended.\n",files);
rewind(fa);
printf("%s contents:\n",argv[1]);
while((ch = getc(fa)) != EOF) putchar(ch);
puts("\nDone displaying.");
fclose(fa);
return 0;
}
void append(FILE * source,FILE * dest){
size_t bytes;
static char temp[BUFSIZE];
while(( bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0){
fwrite(temp,sizeof(char),bytes,dest);
}
}
char * s_gets(char * st,int n){
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if(ret_val){
find = strchr(st,'\n');
if(find) *find = '\0';
else{
while(getchar() != '\n')
continue;
}
}
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈