修改下面的程序,使用命令行参数(而不是交互式界面)获得文件名。
/* append.c -- 把多个文件的内容追加到一个文件中 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main (viod)
{
FILE *fa, *fs; // fa指向追加的目的文件,fs指向源文件
int files = 0; // 追加文件的个数
char files_app[SLEN]; // 被追加文件的名称
char files_src[SLEN]; // 源文件的名称
puts("Enter name of destination files: ");
gets(files_app);
if((fa = fopen(files_app, "a")) == NULL)
{
fprintf(stderr, "Can't open %s\n", file_app);
exit(2);
}
if(setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
puts("Enter name of first source file(empty line to quit): ");
while(gets(file_src) && 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("Can't create input buffer\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 appanded.\n", file_src);
puts("Next file (empty line to quit): ");
}
}
printf("Done. %d files appended.\n", files);
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);
}

/* append.c -- appends files to a file */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define BUFSIZE 1024 #define SLEN 81 void append(FILE *source, FILE *dest); int main(int argc, char *argv[]) { FILE *fa, *fs; // fa for append file, fs for source file int files = 0,i; // number of files appended if ((fa = fopen(argv[1], "a")) == NULL) { fprintf(stderr, "Can't open %s\n", argv[1]); exit(2); } if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0) { fputs("Can't create output buffer\n", stderr); exit(3); } for (i=2; i<argc ;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("Can't create input buffer\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. %d files appended.\n", files); fclose(fa); return 0; } void append(FILE *source, FILE *dest) { size_t bytes; static char temp[BUFSIZE]; // allocate once while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0) fwrite(temp, sizeof (char), bytes, dest); }