我一直在研究一个读取不同货币文件的数据结构程序,然后我将其用于转换。我已经连续几天跑了过来,并尝试了fgets
,fscanfs
等等,而我现在只是因为我对编程很陌生而迷失了。
数据。文件在如下的单独行中列出:
dollar 1.00
yen 0.0078
franc 0.20
mark 0.68
pound 1.96
到目前为止我的代码:
typedef struct {
string currency;
double rate;
} currencyT;
typedef struct {
currencyT cur[MaxCurrencyTypes];
int nCurrency;
} *currencyDB;
static currencyDB ReadDataBase(void);
static ReadOneLine(FILE *infile, currencyDB db);
static currencyDB ReadDataBase(void)
{
FILE *infile;
currencyDB db;
int nCurrency;
db = New(currencyDB);
infile = fopen(ExchangeFile, "r");
while (ReadOneLine(infile, db));
fclose(infile);
return(db);
}
static ReadOneLine(FILE *infile, currencyDB db)
{
currencyT cur;
char termch, currency;
double rate;
int nscan, nCurrency;
nCurrency = 0;
while(1) {
nscan = fscanf(infile, "%20s %f%c", db->cur[nCurrency].currency,
&db->cur[nCurrency].rate, &termch);
if(nscan = EOF) break;
if(nscan != 3 || termch != '\n') {
Error("Improper file format");
}
nCurrency++;
}
db->nCurrency = nCurrency;
}
static void ProcessExchange(currencyDB db)
{
}
main()
{
currencyDB currencies;
currencies = ReadDataBase();
ProcessExchange(currencies);
}
答案 0 :(得分:0)
见8&首先是9。
MaxCurrencyTypes
未声明,我们假设
#define MaxCurrencyTypes (5)
string
未声明,我们假设
typedef char * string;
currencyDB
是指针类型,建议改为声明为结构类型
typedef struct {
currencyT cur[MaxCurrencyTypes];
int nCurrency;
} currencyDB; // Drop *
static ReadOneLine()
应使用显式返回类型。
static int ReadOneLine()
int nCurrency;
未在ReadDataBase()
中使用。
New()
中db = New(currencyDB)
未声明或定义。假设为*db
分配未初始化的内存。
ExchangeFile
未声明。
if (nscan = EOF)
- > if (nscan == EOF)
ReadOneLine()
需要返回一个值。
main()
应明确说明返回类型和参数。
int main(void)