The CSV File

Of all the common file formats, the CSV is probably the oldest one still in use. It’s a plain text file, so the formatted data appears is readable by humans: Each line is a record. Each field is separated by a single comma.

Other plain text file formats are popular today, including XML and JSON, but CSV is easy to read and write in C without having to use external libraries.

Right-click this link to download a typical CSV file. This one stores dates and temperatures from this year’s brutal winter in my home town. The data is formatted as follows:

(int)year, (int)month, (int)day, (float)high, (float)low

When you know what the fields represent, the CSV file makes sense; otherwise it’s just a list of meaningless numbers. In this case, the format uses numeric values (no strings). Some values are integers, others real numbers (floats). Strings in a CSV file are usually enclosed in double quotes, though this practice isn’t consistent. Regardless, for this month’s Exercise, the data are all numbers.

Your task is to create code that reads the CSV file, weather_data.csv (link). Each line is processed and presented in the following output format:

  December 25 2016:     High 29.0      Low 19.0

This task portends a more complex program that I offer as next month’s Exercise. Until then, on your own, write the code to process the CSV file.

(Click here to view my solution, but please try the exercise on your own before you see what I did.)

Leave a Reply