Parsing File Input

The read_weather application is fine if all you want to do is print the contents of a file verbatim. However, if you'd like to do more with your stored data, you'll need a method of parsing the read lines into their respective parts.

This can be accomplished using the sscanf() function. This function, first introduced in Chapter 5, can break one string down into different pieces, using formatting parameters like those used with printf() and fprintf(). The basic syntax is

sscanf(string_to_parse, formatting,
					
					arguments…);

Obviously the number of arguments depends on how many elements are expected to be extracted from string_to_parse. Our next example will rework read_weather, breaking each line into its date, high temperature, and low temperature, just as they were first entered.

To parse file input

1.
Open read_weather.c (Script 12.3) in your text editor or IDE.

2.
After defining the line variable, define three more variables (Script 12.4):

int high, low;
char date[11];

Script 12.4. This variation on the read_weather application uses the sscanf() function to break the stored data into its individual parts.


These lines should look familiar: they're taken straight out of the record_weather application (Script 12.2).

3.
Change the caption (inside the fp conditional) to read

printf("%10s %5s %5s
", "Date",
 "High", "Low");

Using a little printf() formatting, this application will print out the retrieved data in columns.

4.
Change the contents of the while loop to read

sscanf (line, "%10s %d %d", date,
 &high, &low);
printf("%10s %5d %5d
", date, high,
 low);

The condition of the while loop will continue to use fgets() to read through the entire file. Within the while loop, the process changes. First, the sscanf() function is used to break the read line into its three distinct parts. This structure parallels that used to key in the data in record_weather.

Then the three variables are printed, using the same formatting as the caption in Step 3.

5.
Save the file as parse_weather.c.

6.
Compile and debug as necessary.

7.
Run the compiled application (Figure 12.9).

Figure 12.9. The application now breaks the file's contents into its individual parts and prints them in a table-like structure.


✓ Tips

  • For more review of sscanf(), see Chapter 5.

  • The sscanf() function returns the number of elements it excised. You could use this information to check that each line contained three distinct elements. If you wanted, you could make the sscanf() line a condition, checking that it returns 3. That way, you could weed out nondata lines in the file, such as comments.

  • Now that you know how to parse file input, you can use your existing knowledge to determine the highest high temperature or the lowest low temperature, or to calculate median temperatures. You could start by building a dynamically sized array (see Chapter 10, “Managing Memory”), populated from the file data, and then perform calculations or sorts on this array.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset