Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

72 Part I — Basics


Listing 5-7(continued)

}

close(DATA);

sub despace
{
my ($text) = @_;
$text =~ s/ *$//g;
return $text;
}

Writing Fixed-Width Files


Writing a fixed-width file, like writing a delimited file, is very straightforward. Once you
know the format of the file, your only real problem is choosing a padding character to use.
An obvious solution in most situations is to use a zero for numerical values and a space for text
values.

If your chosen language supports the printf()or sprintf()functions (both part of the
standard C library), you can use the formatter to align the data for you. For example, you can
output a record with two 10-character fixed-width numbers and three 20-character-wide
fixed-width text fields with the line in Listing 5-8.

Listing 5-8:Writing a Fixed-Width Record

printf(‘%010d%010d%-20s%-20s%-20s’,145,1385674,’Martin’,’Brown’,’United
Kingdom’);

The code in Listing 5-8 will work in Perl and C (with the necessary headers and wrappers
added).

Updating Text Files


As mentioned earlier in the chapter, updating a text file is a matter of either copying informa-
tion up to the point where you want to update, or of inserting information before that point,
writing the new information, and then copying the rest of the file. Once you get to this stage,
however, it really is more effective to use an alternative method to store the information.
Free download pdf