Using %s
with scanf
means to read a word. It stops when it hits a space. However, your sourcefile contains some spaces inside the things you are trying to read. For example, WASHINGTON
and NEW YORK
both need to be read into city
.
You have to read an exact number of characters from your file, instead of using %s
.
One way to do that is to, in the fscanf
only, change %11s
to use this specifier: %11[^\n]
. That means to read the next 11 characters (unless we hit the end of the line), even if there are spaces in it.
This will actually put trailing spaces into your strings, so if you don't want the trailing spaces then you will need to make another function which deletes them from your string.
You should also check the return value of fscanf
. Since you are trying to read 11 items, it should return 11
. If it returns something else then your program should report a data error and stop reading.
NB. For testing purposes, in your printf
, use commas or something, instead of spaces, between the items; this will make it easier to identify whether the data is being read that way you expect.