0

My assignment is to take input from a txt file and read that into parts of a struct array, then manipulate it, but I'm having trouble with reading from the file.

my struct is as follows:

   typedef struct Employee
   {
        char first[7], initial, last[9], street[16], city[11], state[2], zip[5];
        int age;
        char sex;
        int tenure;
        float salary;
   } Employee;

and within my main: I have defined MAX to be 100

FILE *payfile;
Employee worker[MAX];
int i = 0, num;
float average;

payfile = fopen("payfile.txt","r");

while(!feof(payfile))
 {
      fscanf(payfile,"%s %c %s %s %s %s %s %d %c %d %f",
        worker[i].first,&worker[i].initial,worker[i].last,worker[i].street,
        worker[i].city,worker[i].state,worker[i].zip,&worker[i].age,&worker[i].sex,
        &worker[i].tenure,&worker[i].salary);
      i++;
 }

here is the txt file we are to read from:

    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

so I'm having some trouble with fscanf accuracy. To check what worker is containing, I'm using printf("%s",worker[0].first) and printf("%c",worker[0].initial) ... etc.

When I scan for the first string in the first line, I get ADA, then scan for whitespace and get the next char, A. Good so far. Then I scan for white space and take the the next string, which returns AGUSTA. But now I run into this problem. I need to get 33 BABBAGE ROAD into worker[0].street, but changing the parameter "%s " to "%16c " doesn't work. It instead includes part of the city.

What am I doing wrong? What do I change to scan the respective lines of code with correct accuracy?

Thanks.

4

2 回答 2

1
char first[7], initial, last[9], street[16], city[11], state[2], zip[5];

如果您想使用字符串(C 字符串)读取,包括 EOS 在内的 char 大小是不够的。

改成

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

以空格分隔的 scanf 读取中的控制字符%s,指定读取大小和未读字符,例如。

    while(EOF!=fscanf(payfile,"%7s %c %9s %16[^\n] %11[^\n] %2s %5s %d %c %d %f",
          worker[i].first,&worker[i].initial,worker[i].last,worker[i].street,
          worker[i].city,worker[i].state,worker[i].zip,&worker[i].age,&worker[i].sex,
          &worker[i].tenure,&worker[i].salary))
    {
        i++;
    }

如果不需要尾随空格,您可以删除。

于 2013-05-12T22:22:05.703 回答
0

您可以将街道读取为一系列字符串,即 %s %s %s。然后用 strcat() 将它们连接成一个字符串。

这不是一个通用的解决方案,因为超过 3 个单词的街道(例如“19 N 3rd street”)将失败。但我怀疑这对你的任务来说已经足够了。在现实生活中的应用程序中,这些字段将使用某种分隔符分隔,通常是逗号(请参阅 .csv)。由于您的申请中似乎不存在这种情况,因此请这样做,然后告诉您的老师将来给您更好的作业。

于 2013-05-12T21:51:04.407 回答