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

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

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

沧海一幻觉 2023-03-16 17:13:04
/* append.c -- appends files to a file */#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; // fa for append file, fs for source fileint files = 0; // number of files appendedchar file_app[SLEN]; // name of append filechar file_src[SLEN]; // name of source fileputs("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 oncewhile ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)fwrite(temp, sizeof (char), bytes, dest);}
查看完整描述

1 回答

?
慕尼黑8549860

TA贡献1818条经验 获得超11个赞

//要另外说下如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 81
void append(FILE *source, FILE *dest);

int main(void)
{
FILE *fa, *fs; //定义2个文件类型指针
int files = 0; // 追加文件个数
char file_app[SLEN];  
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); 
}
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++;//追加文件数+1
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]; 

while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)//把源文件的内容追加到追加文件,块大小sizeof(char),块数为BUFSIZE
fwrite(temp, sizeof (char), bytes, dest);//写文件块大小sizeof(char),块数为BUFSIZE
}

 


查看完整回答
反对 回复 2023-03-18
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

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