可以帮我详细说下程序的流程,文件函数怎样操作的?

/* 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(void)
{
FILE *fa, *fs; // fa for append file, fs for source file
int files = 0; // number of files appended
char file_app[SLEN]; // name of append file
char file_src[SLEN]; // name of source file
puts("Enter name of destination file:");
gets(file_app);
if ((fa = fopen(file_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 appended.\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]; // allocate once

while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
fwrite(temp, sizeof (char), bytes, dest);
}

沧海一幻觉
浏览 84回答 1
1回答

慕尼黑8549860

//要另外说下如fprintf(stderr, "Can't open %s\n", file_app);这是向文件或者系统设备输出的函数;但他的文件指针为stderr;这是c中的标准错误输出设备指针,系统自动分配为显示器故相当于printf("Can't open %s\n", file_app);#include <stdio.h>#include <stdlib.h>#include <string.h>#define BUFSIZE 1024#define SLEN 81void append(FILE *source, FILE *dest);int main(void){FILE *fa, *fs; //定义2个文件类型指针int files = 0; // 追加文件个数char file_app[SLEN];&nbsp;&nbsp;char file_src[SLEN]; // 2个字符串用来储存文件名;puts("Enter name of destination file:");//输出Enter name of destination file:gets(file_app);//输入要追加的文件名if ((fa = fopen(file_app, "a")) == NULL)//fa指向追加的目的文件,以追加方式打开文件,如果打开失败退出;{fprintf(stderr, "Can't open %s\n", file_app);exit(2);&nbsp;}if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)//创建缓冲器与流相关,大小为BUFSIZE,作用是提高IO速度;如果打开失败退出{fputs("Can't create output buffer\n", stderr);exit(3);}puts("Enter name of first source file (empty line to quit):");//输出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)//创建缓冲器与流相关,大小为BUFSIZE,作用是提高IO速度;如果打开失败开始下次循环{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++;//追加文件数+1printf("File %s appended.\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];&nbsp;while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)//把源文件的内容追加到追加文件,块大小sizeof(char),块数为BUFSIZEfwrite(temp, sizeof (char), bytes, dest);//写文件块大小sizeof(char),块数为BUFSIZE}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP