//io.cpp
#include "stdafx.h"
#include "test.h"#include "io.h"
int ReadFromFile( char *FileName,
USER_T *ptheKind, /* 电表用户的类型*/
USE_TIME_t *pStart_Time, /* 开始使用时间 */WEEK_T *pStart_Week, /* 开始使用时间是星期几 */
USE_TIME_t *pEnd_Time, /* 结束使用时间 */
int *pValue /* 用电量 */
);
//函数名称:Input()
int Input( USER_T *ptheKind, /* 电表用户的类型 */
USE_TIME_t *pStart_Time, /* 开始使用时间 */
WEEK_T *pStart_Week, /* 开始使用时间是星期几 */
USE_TIME_t *pEnd_Time, /* 结束使用时间 */
int *pValue /* 用电量 */
)
{
static bool flag = false;
if ( ptheKind == NULL || pStart_Time == NULL || pStart_Week == NULL || pEnd_Time == NULL || pValue == NULL )
{
return -1;
}
if ( flag == false )
{
ReadFromFile("E:\\input.txt",ptheKind, pStart_Time, pStart_Week, pEnd_Time, pValue);
flag = true;
}
return 0;
}
int ReadFromFile(char *FileName,
USER_T *ptheKind, /* 电表用户的类型 */
USE_TIME_t *pStart_Time, /* 开始使用时间 */
WEEK_T *pStart_Week, /* 开始使用时间是星期几 */
USE_TIME_t *pEnd_Time, /* 结束使用时间 */
int *pValue /* 用电量 */
)
{
int i;
int a = 0;
FILE* fp;
fp = fopen(FileName,"r");
if (fp == NULL)
{
printf(FileName);
printf("doesn't exist\n");
fclose(fp);
return -1;
}
for (i = 0; ; )
{
/* 电表用户的类型 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
*ptheKind = (USER_T)a;
/* 开始使用时间 年 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pStart_Time->year = a;
/* 开始使用时间 月 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pStart_Time->month = a;
/* 开始使用时间 日 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pStart_Time->date = a;
/* 开始使用时间是星期几 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
*pStart_Week = (WEEK_T)a;
/* 结束使用时间 年 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pEnd_Time->year = a;;
/* 结束使用时间 月 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pEnd_Time->month = a;
/* 结束使用时间 日 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
pEnd_Time->date = a;
/* 用电量 */
if (fscanf(fp,"%d", &a)== EOF)
{
break;
}
*pValue = (int)a;
}
fclose(fp);
return 0;
}
//函数名称:Output()
int Output( int Fare /* 计算出所花费用;单位:元 */
)
{
FILE* fp;
fp = fopen("E:\\Result.txt","w");
if (fp == NULL)
{
printf("File can't be created\n");
fclose(fp);
return -1;
}
{
fprintf(fp,"%d\n", Fare);
}
fclose(fp);
return 0;
}