《C和指针》课后题15.5 文件操作问题

guocai_yao guocai_yao 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
要求: 复制文件内容,并在输出文件中的结尾追加输入文件中出现的数字之和
问题: 当输入文件中出现数字时,输出文件的末尾并没有追加数字之和,而只是个0
IDE: VC6

/******************************************************************************************
**FileName : file_read_write.c
#include <stdio.h>

#define BUFSIZE 256

/******************************************************************************************
*函数名称:FILE * open_file(char *promt, char *mode)
*函数功能:打开文件
*入口参数:promt, 文件的名称; mode, 文件的打开方式
*返 回 值:FILE型指针
*备 注:无
*******************************************************************************************/
FILE * open_file(char *promt, char *mode)
{
char buf[BUFSIZE];
FILE *file;

printf("%s filename is:", promt);

if(gets(buf) == NULL){
fprintf(stderr, "Missing %s filename.\n", promt);
//exit(EXIT_FAILURE);//为什么用exit函数会报错未定义?
}

if((file = fopen(buf, mode)) == NULL){
perror(buf);
// exit(EXIT_FAILURE);
}

return file;
}

int main()
{
char buf[BUFSIZE];
FILE *input;
FILE *output;
int value ;
int total = 0;

input = open_file("Input", "r");
output = open_file("Output", "w");

while(fgets(buf, BUFSIZE, input) != NULL){
if(sscanf(buf, "%d", &value) == 1)
total += value;
fputs(buf, output);
}
fprintf(output, "%d\n", total);

fclose(output);
fclose(input);

return 0;
}
...全文
评论 16
收藏 已收藏
分享
返回顶部
guocai_yao
C语言 70,039 成员 243,237 内容