69,364
社区成员
发帖
与我相关
我的任务
分享
/******************************************************************************************
**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;
}
// 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;
}
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;
}
#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;
}