亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

可以幫我詳細說下程序的流程,文件函數怎樣操作的?

可以幫我詳細說下程序的流程,文件函數怎樣操作的?

滄海一幻覺 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 關注
  • 99 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號