第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

可以幫我詳細(xì)說(shuō)下程序的流程,文件函數(shù)怎樣操作的?

可以幫我詳細(xì)說(shuō)下程序的流程,文件函數(shù)怎樣操作的?

滄海一幻覺(jué) 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貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超11個(gè)贊

//要另外說(shuō)下如fprintf(stderr, "Can't open %s\n", file_app);這是向文件或者系統(tǒng)設(shè)備輸出的函數(shù);但他的文件指針為stderr;這是c中的標(biāo)準(zhǔn)錯(cuò)誤輸出設(shè)備指針,系統(tǒng)自動(dòng)分配為顯示器故相當(dāng)于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個(gè)文件類型指針
int files = 0; // 追加文件個(gè)數(shù)
char file_app[SLEN];  
char file_src[SLEN]; // 2個(gè)字符串用來(lái)儲(chǔ)存文件名;
puts("Enter name of destination file:");//輸出Enter name of destination file:
gets(file_app);//輸入要追加的文件名
if ((fa = fopen(file_app, "a")) == NULL)//fa指向追加的目的文件,以追加方式打開(kāi)文件,如果打開(kāi)失敗退出;
{
fprintf(stderr, "Can't open %s\n", file_app);
exit(2); 
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)//創(chuàng)建緩沖器與流相關(guān),大小為BUFSIZE,作用是提高IO速度;如果打開(kāi)失敗退出
{
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')//輸入源文件如果是空串結(jié)束循環(huán)
{
if (strcmp(file_src, file_app) == 0)//如果源和追加文件相同
fputs("Can't append file to itself\n",stderr);
else if ((fs = fopen(file_src, "r")) == NULL)//如果打開(kāi)源文件失敗
fprintf(stderr, "Can't open %s\n", file_src);
else
{
if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)//創(chuàng)建緩沖器與流相關(guān),大小為BUFSIZE,作用是提高IO速度;如果打開(kāi)失敗開(kāi)始下次循環(huán)
{
fputs("Can't create input buffer\n",stderr);
continue;
}
append(fs, fa);//函數(shù)
if (ferror(fs) != 0)//檢查文件操作是否有錯(cuò)
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);//關(guān)閉源文件
files++;//追加文件數(shù)+1
printf("File %s appended.\n", file_src);
puts("Next file (empty line to quit):");
}
}
printf("Done. %d files appended.\n", files);
fclose(fa);//關(guān)閉追加文件

return 0;
}

void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE]; 

while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)//把源文件的內(nèi)容追加到追加文件,塊大小sizeof(char),塊數(shù)為BUFSIZE
fwrite(temp, sizeof (char), bytes, dest);//寫(xiě)文件塊大小sizeof(char),塊數(shù)為BUFSIZE
}

 


查看完整回答
反對(duì) 回復(fù) 2023-03-18
  • 1 回答
  • 0 關(guān)注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)