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

guocai_yao 2009-03-17 10:22:34
要求: 复制文件内容,并在输出文件中的结尾追加输入文件中出现的数字之和
问题: 当输入文件中出现数字时,输出文件的末尾并没有追加数字之和,而只是个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;
}
...全文
182 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
guocai_yao 2009-03-18
  • 打赏
  • 举报
回复
自己顶一个
guocai_yao 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 pbdwadr 的回复:]
我觉得题目描述的很模糊
1.数字是什么类型?整形还是浮点数?
2.“abc32”这样的字符串是提取32(三十二)还是提取3和2?
[/Quote]
1. 数字是整型的
2. 应该提取3和2
pbdwadr 2009-03-17
  • 打赏
  • 举报
回复
我觉得题目描述的很模糊
1.数字是什么类型?整形还是浮点数?
2.“abc32”这样的字符串是提取32(三十二)还是提取3和2?
insulted 2009-03-17
  • 打赏
  • 举报
回复
问题转换成:
在一个包含有数字的字符(各种字符,包含空格(数量不定),)串中,如何提取出数字。
insulted 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 guocai_yao 的回复:]
7楼的也不对啊 计算的值不对
[/Quote]
注意数字前面的空格的个数!一个,两个,也可能是多个!
7楼的code,一个空格算两次数字,两个空格算三次数字,...,n个空格算n+1次数字!
guocai_yao 2009-03-17
  • 打赏
  • 举报
回复
7楼的也不对啊 计算的值不对
conquer 2009-03-17
  • 打赏
  • 举报
回复
可以编译的版本

// nbbb.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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");
input = fopen("D:/Temp/nbbb/Debug/input.txt","r");
if(NULL == input )
{
return 0;
}
output = fopen("D:/Temp/nbbb/Debug/output.txt","w");
if(NULL == output)
{
return 1;
}
while(fgets(buf, BUFSIZE, input) != NULL){
char *p = buf;
while('\0' != *p)
{
if(sscanf(p, "%d", &value) == 1)
total += value;
p++;
}
fputs(buf, output);
}
fprintf(output, "%d\n", total);

fclose(output);
fclose(input);

return 0;
}

lwh_1024 2009-03-17
  • 打赏
  • 举报
回复
不好意思,刚才说错了。
应该是缺少头文件stdlib.h
conquer 2009-03-17
  • 打赏
  • 举报
回复




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)
{
char *p = buf;
while('\0' != *p)
{
if(sscanf(p, "%d", &value) == 1)
total += value;
p++;
}
fputs(buf, output);
}

fprintf(output, "%d\n", total);

fclose(output);
fclose(input);

return 0;
}


原来的代码是因为sscanf把整个读出来的buf的第一个字符当%d读,就一次,成功可以记录,不成功放弃整个串,应该循环串里的每个字符。
lwh_1024 2009-03-17
  • 打赏
  • 举报
回复
是报EXIT_FAILURE没有被定义吧?
如果的确是报函数exit没有被定义,我估计和你的环境有关系了
insulted 2009-03-17
  • 打赏
  • 举报
回复
帮顶!
guocai_yao 2009-03-17
  • 打赏
  • 举报
回复
刚才贴上去的时候没贴好
guocai_yao 2009-03-17
  • 打赏
  • 举报
回复
要求: 复制文件内容,并在输出文件中的结尾追加输入文件中出现的数字之和
问题: 当输入文件中出现数字时,输出文件的末尾并没有追加数字之和,而只是个0
IDE: VC6

#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;
}
guocai_yao 2009-03-17
  • 打赏
  • 举报
回复
加了 代码上忘了忘了
wuyu637 2009-03-17
  • 打赏
  • 举报
回复
头文件没有加
#include <stdio.h>
pjxajmj 2009-03-17
  • 打赏
  • 举报
回复
up up

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧