I'm doing the exercises in a book, Head First C . I double-checked that my code is the same as in the book and I'm carefully following the steps in the book.
The C file is supposed to take gps data and format it in JSON style.
#include <stdio.h>
int main() {
float latitude;
float longitude;
char info[40];
int started = 0;
puts("data=[");
while (scanf("%f, %f, %39[^\n]", &latitude, &longitude, info) ==3) {
if (started)
printf(", \n");
else
started = 1;
printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info);
}
puts("\n]");
return 0;
}
First the book had us type in the data, to show us how tedious and poorly formatted it looks, then we learn to redirect stdin to read a file of comma-separated data, like so:
$ ./geo2json < gpsdata.csv
I couldn't find source files online so I copy/pasted from the pdf of the book to create the csv file.
gpsdata.csv and gpsdata.txt
42.363400,-71.098465,Speed = 21
42.363327,-71.097588,Speed = 23
42.363255,-71.096710,Speed = 17
42.363182,-71.095833,Speed = 22
42.363110,-71.094955,Speed = 14
42.363037,-71.094078,Speed = 16
42.362965,-71.093201,Speed = 18
42.362892,-71.092323,Speed = 22
42.362820,-71.091446,Speed = 17
42.362747,-71.090569,Speed = 23
42.362675,-71.089691,Speed = 14
42.362602,-71.088814,Speed = 19
42.362530,-71.087936,Speed = 16
42.362457,-71.087059,Speed = 16
42.362385,-71.086182,Speed = 21
Weird stuff happened, so I went thru the lines and deleted then re-entered the line breaks in case there was some hidden formatting. I tried changing the file type from .csv to .txt. Neither made a difference. When I typed in the data, this weirdness did not happen.
How it's supposed to look:
How it actually looks. Note how the last line is different. BTW sometimes the 'l' in latitude shows up on that last line.
login:~/programming/cfiles> ./geo2json < gpsdata.csv
data=[
'}, itude: 42.363400, longitude: -71.098465, info: 'Speed = 21
'}, itude: 42.363327, longitude: -71.097588, info: 'Speed = 23
'}, itude: 42.363255, longitude: -71.096710, info: 'Speed = 17
'}, itude: 42.363182, longitude: -71.095833, info: 'Speed = 22
'}, itude: 42.363110, longitude: -71.094955, info: 'Speed = 14
'}, itude: 42.363037, longitude: -71.094078, info: 'Speed = 16
'}, itude: 42.362965, longitude: -71.093201, info: 'Speed = 18
'}, itude: 42.362892, longitude: -71.092323, info: 'Speed = 22
'}, itude: 42.362820, longitude: -71.091446, info: 'Speed = 17
'}, itude: 42.362747, longitude: -71.090569, info: 'Speed = 23
'}, itude: 42.362675, longitude: -71.089691, info: 'Speed = 14
'}, itude: 42.362602, longitude: -71.088814, info: 'Speed = 19
'}, itude: 42.362530, longitude: -71.087936, info: 'Speed = 16
'}, itude: 42.362457, longitude: -71.087059, info: 'Speed = 16
'}atitude: 42.362385, longitude: -71.086182, info: 'Speed = 21
]
I checked the online errata and didn't see anything about this. I was using Cygwin but then tried linux, both had the same result.
Any help or insight would be appreciated!
Your file contains Windows-style CRLF line endings and your program is not equipped to deal with them. The authors of this book have assumed a Linux or Mac machine (LF newlines) without testing on Windows-style text files. The result is that each line's carriage return (CR) character is still contained in the string info
, which screws up the output.
You can modify the program to scan for both CR and LF characters by
scanf("%f, %f, %39[^\r\n]", &latitude, &longitude, info)
Note the \\r
.
(You say you tried this on Linux, but I guess you used the input CSV file that you made on Windows?)
Try changing
while (scanf("%f, %f, %39[^\n]", &latitude, &longitude, info) ==3) {
to
while (scanf("%f, %f, %39[^\n\r]", &latitude, &longitude, info) ==3) {
I think you may have returns in your input file (was it from a Windows box?).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.