诸如pay
——明确定义为FILE *pay;
——的全局变量是个坏主意,在示例代码中完全没有必要。始终测试来自fscanf()
及其亲属的返回值,以确保您获得了预期的所有数据。
但是,您的问题是%s
停在第一个空格处,因此您在读取地址字段时遇到了巨大的问题。您的输入也不受限制。您还尝试多次使用街道地址的多个单词e->street
;这行不通,因为第三个单词会覆盖第一个单词。
你需要类似的东西:
int inputLine(FILE *fp, Employee* e)
{
if (fscanf(fp, "%7s %c %9s %16c %11c %2s %d %d %c %d %f",
e->first, &e->initial, e->last, e->street, e->city, e->state,
&e->zip, &e->age, &e->sex, &e->tenure, &e->salary) != 11)
return -1;
e->street[16] = '\0';
e->city[11] = '\0';
return 0;
}
这用于%c
读取单个字符;它用于%16c
读取多字街道地址和%11c
读取(可能是多字)城市。它使用%7s
,%9s
和%2s
来防止其他字符串溢出。调用后的赋值fscanf()
确保计数的%c
字符串为空终止;本身%16c
不添加空终止符。
该inputLine()
函数现在在出现问题时返回错误指示(已-1
选择),并0
指示成功。这是 Unix 系统调用的常见模式,但与底层scanf()
-family 函数的行为不同,如chux的评论中所述。
工作代码 1
此代码使用问题中的 as 读取标准输入fscanf()
。它也确保emp
数组没有溢出。
#include <stdio.h>
typedef struct
{
char first[8], initial, last[10],
street[17], city[12], state[3];
int age, tenure, zip;
float salary;
char sex;
} Employee;
void dump_employee(FILE *fp, const char *tag, const Employee *e);
int inputLine(FILE *fp, Employee *e);
enum { MAXEMP = 10 };
int main(void)
{
char line[4096];
Employee emp[MAXEMP];
if (fgets(line, sizeof(line), stdin) == 0 ||
fgets(line, sizeof(line), stdin) == 0)
return 1;
for (int i = 0; i < MAXEMP && inputLine(stdin, &emp[i]) != 0; i++)
dump_employee(stdout, "Employee", &emp[i]);
return 0;
}
int inputLine(FILE *fp, Employee *e)
{
if (fscanf(fp, "%7s %c %9s %16c %11c %2s %d %d %c %d %f",
e->first, &e->initial, e->last, e->street, e->city, e->state,
&e->zip, &e->age, &e->sex, &e->tenure, &e->salary) != 11)
return -1;
e->street[16] = '\0';
e->city[11] = '\0';
return 0;
}
void dump_employee(FILE *fp, const char *tag, const Employee *e)
{
fprintf(fp, "%s: %-7s %c %-9s %-16s %-11s %-2s %.5d %3d %c %d %6.2f\n",
tag, e->first, e->initial, e->last, e->street, e->city, e->state,
e->zip, e->age, e->sex, e->tenure, e->salary);
}
样本输出
Employee: ADA A AGUSTA 33 BABBAGE ROAD LOVELACE GB 19569 28 F 2 350.50
Employee: ISSAC A ASIMOV 99 FICTION WAY AMHERST MA 63948 58 M 6 423.88
Employee: HUMPHRY R BOGART 71 SAM STREET HOLLYWOOD CA 48482 56 M 5 366.00
工作代码 2
此代码用于fgets()
读取行和sscanf()
转换数据。使用此版本的代码可以更轻松地报告错误。
#include <stdio.h>
typedef struct
{
char first[8], initial, last[10],
street[17], city[12], state[3];
int age, tenure, zip;
float salary;
char sex;
} Employee;
void dump_employee(FILE *fp, const char *tag, const Employee *e);
int scan_employee(Employee *e, const char *line);
enum { MAXEMP = 10 };
int main(void)
{
char line[4096];
Employee emp[MAXEMP];
if (fgets(line, sizeof(line), stdin) == 0 ||
fgets(line, sizeof(line), stdin) == 0)
return 1;
for (int i = 0; i < MAXEMP && fgets(line, sizeof(line), stdin) != 0; i++)
{
if (scan_employee(&emp[i], line) == 0)
dump_employee(stdout, "Employee", &emp[i]);
}
return 0;
}
int scan_employee(Employee *e, const char *line)
{
if (sscanf(line, "%7s %c %9s %16c %11c %2s %d %d %c %d %f",
e->first, &e->initial, e->last, e->street, e->city, e->state,
&e->zip, &e->age, &e->sex, &e->tenure, &e->salary) != 11)
return -1;
e->street[16] = '\0';
e->city[11] = '\0';
return 0;
}
void dump_employee(FILE *fp, const char *tag, const Employee *e)
{
fprintf(fp, "%s: %-7s %c %-9s %-16s %-11s %-2s %.5d %3d %c %d %6.2f\n",
tag, e->first, e->initial, e->last, e->street, e->city, e->state,
e->zip, e->age, e->sex, e->tenure, e->salary);
}
对于问题中的样本数据,此输出与另一个输出相同。