Note that there are some explanatory texts on larger screens.

plurals
  1. POReading from .txt file using fscanf in C
    primarykey
    data
    text
    <p>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.</p> <p>my struct is as follows:</p> <pre><code> 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; </code></pre> <p>and within my main: I have defined MAX to be 100</p> <pre><code>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,&amp;worker[i].initial,worker[i].last,worker[i].street, worker[i].city,worker[i].state,worker[i].zip,&amp;worker[i].age,&amp;worker[i].sex, &amp;worker[i].tenure,&amp;worker[i].salary); i++; } </code></pre> <p>here is the txt file we are to read from:</p> <pre><code> 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 </code></pre> <p>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.</p> <p>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.</p> <p>What am I doing wrong? What do I change to scan the respective lines of code with correct accuracy?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload