我正确使用这个结构数组吗?

时间:2014-05-11 23:54:24

标签: c arrays structure

我只是想知道我是否正确地将值存储到此数组中,我尝试使用 - >运营商,但我收到了一个错误。我主要担心的是扫描功能,我不确定使用数字[i]是否有效。

来自payfile.txt的文字:

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
ALBERT  G EINSTEIN  94 ENERGY WAY    PRINCETON   NJ 47474 67 M 8 780.00
EMMYLOU L HARRIS    66 COUNTRY ROAD  NASHVILLE   TN 72647 38 F 2 767.42
JAMES   T KIRK      11 SPACE STREET  VULCAN      CA 82828 46 M 1 235.70
TED     L KOPPEL    55 ABC PLACE     WASHINGTON  DC 37376 48 M 9 909.44
DAVID   T LETTERMAN 14 WNBC AVENUE   NEW YORK    NY 19338 47 M 5 445.65
STEVIE  R NICKS     31 MUSIC ROAD    CHICAGO     IL 23459 38 F 8 460.88
MONTY   P PYTHON    76 SILLY STREET  LONDON      GB 80939 44 M 2 320.50
ROGER   R RABBIT    15 LOONEY TOONS  HOLLYWOOD   CA 91343 24 M 4 259.53
SALLY   W RIDE      21 COLUMBIA WAY  HOUSTON     TX 91123 30 F 9 707.80
ROD     Q SERLING   11 TWLIGHT ZONE  SAN DIEGO   CA 93939 56 M 1 440.00
LUKE    R SKYWALKER 43 MILKY WAY     NEW YORK    NY 12343 35 M 5 660.00

源文件:

#include <stdio.h>

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

void readData(Employee *number);

int main(void){
    Employee number[14];

    readData(number);
    getchar();
    return 0;
}

void readData(Employee *number){
    int i;
    FILE *f = fopen("payfile.txt", "r");
    for (i = 0; i < 14; i++) {
        fscanf(f, "%7s %c %9s %16s %11s %2s %5s %d %c %d %lf\n", number[i].first, &number[i].initial, number[i].last, number[i].street, number[i].city, number[i].state, number[i].zip, &number[i].age, &number[i].sex, &number[i].tenure, &number[i].salary);
        printf("%s %c %s %s %s %s %s %d %c %d %f\n", number[i].first, number[i].initial, number[i].last, number[i].street, number[i].city, number[i].state, number[i].zip, number[i].age, number[i].sex, number[i].tenure, number[i].salary);
    }
}

0 个答案:

没有答案