C 从文件中读取空格分隔值

标签 c file fgets scanf

我需要从文件中读取文本并根据读取的信息为结构分配值。

这是文本文件的格式:

First Middle Last   Address          city     state zip age sex tenure salary  
\--------------------------------------------------------------  
ADA     A AGUSTA    33 BABBAGE ROAD  LOVELACE    GB 19569 28 F 2 350.50  
ISSAC   A ASIMOV    99 FICTION WAY   AMHERST     MA 63948 58 M 6 423.88  
HUMPHRY R BOGART    71 SAM STREET    HOLLYWOOD   CA 48482 56 M 5 366.00  

我必须将其读入的结构:

typedef struct  
{  
char first[8], initial, last[10],  
street[17], city[12], state[3];  
int age, tenure, zip;  
float salary;  
char sex;  
}Employee;  

到目前为止我使用的代码是:

void inputLine(Employee* e)  
{  
fscanf(pay, "%s %s %s %s %s %s %s %s %d %d %s %d %f",  
           e->first, &e->initial, e->last, e->street,  
           e->street, e->city, e->city, e->state,  
           &e->zip, &e->age, &e->sex, &e->tenure,  
           &e->salary);  
}  

但是当我打印每一行时,前几行没问题,然后事情开始变得困惑:

TED L KOPPEL ABC WASHINGTON DC 37376 48 M 9 909.44  
DAVID T LETTERMAN WNBC NEW YORK 0 0   
NY 1 47 5 STEVIE R 0 0   
NICKS 3 MUSIC CHICAGO 23459 38 0 0   
F 8 460.88 P 76 SILLY 0 89   
STREET L GB 44 2 320.50 0 12341   

我也试过:

void inputLine(Employee* e)  
{  
char line[53];  
fgets(line, 52, pay);  
printf("%s\n", line);  
fflush(stdout);  
sscanf(line, "%s %s %s %s %s %s %s %s %d %d %s %d %f",  
e->first, &e->initial, e->last, e->street,  
e->street, e->city, e->city, e->state,  
&e->zip, &e->age, &e->sex, &e->tenure,  
&e->salary);  
}  

但这给出了相同的结果。

代码不能使用 C++。 我使用的是 Linux GNU GCC 4.8.1 编译器,但我也在 Mac 的编译器上测试过它,但它没有工作。

最佳答案

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 字符串以 null 终止; %16c 本身不添加空终止符。

inputLine() 函数现在会在出现问题时返回错误指示(已选择 -1),而 0 则表示成功。这是 Unix 系统调用的常见模式,但不同于底层 scanf() 函数族的行为,如 comment 中所述通过 chux .

工作代码1

此代码读取标准输入,使用问题中的 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);
}

对于问题中的示例数据,此输出与另一个输出相同。

关于C 从文件中读取空格分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18036686/

相关文章:

C编程数据输入错误

c - fgets 函数的行为很奇怪

C语言 : error: ‘O_DIRECTORY’ undeclared and accessing files in directory and sub-directory(without using opendir())

c - 在c中打印二维数组的段错误

c - 如何创建 int 文件

ios - 我可以创建全局 IBOutlets 和函数吗?

c - 在 Eclipse 中将 fgets(char* c, int i, file* f) 与 printf() 结合使用 - CDT。输出顺序不正确。!

c++ - C 和 C++ 缺乏垃圾回收

c - 我不明白的预处理器宏

java - 在 Java 中将资源文件夹目录从目标类更改为源