Text Files

The first type of file that every programmer writes to and reads from is an ASCII text file. A text file is a kind of computer file that is structured as a sequence of lines. Each line is terminated with a special character such as a line feed (LF) in UNIX or a carriage return (CR) and line feed (LF) in MS DOS.

One special type of text file is called a Comma Seperated Value (CSV) file. CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes.

In real life practice, CSV files can be problematic if care is not given to scrub special characters from the data fields before writing to the file.

The write sample program creates a CSV file representing the first five US Presidents demographic information. I choose to use the open, print, and close commands which are part of the PERL language during coding.

I used the delimited text package (Text::Delimited) that I down loaded from CPAN to create the read text file program. There is a bug in the perl package installer from Active State. Afer commenting out the offending line in web.pm, I used the command line ‘ppm install Text::Delimited’ to add the package.

The read sample program prints the file information to the MS DOS screen.

The table below has starter code that you can use in your next PERL Script.

tst-read-text-file.pl Read Sample Program
tst-write-text-file.pl Write Sample Program
read.cmd Read Batch File
write.cmd Write Batch File
presidents CSV Sample File





In summary, writing to and reading from ASCII text files is very easy. The supplied code can be used to create files of many different formats such as Tab Delimited and Comma Seperated Values to name a few.

Related posts

Leave a Comment