Chapter II.8. Reading and Saving Files

Almost every program needs to save data. Spreadsheets need to save numbers and formulas, word processors need to store text, databases need to store names and addresses, and even video games need to store the top ten highest scores.

To save data, programs store information in a file. After a program stores data in a file, it eventually needs to open that file and retrieve that data again. To save data in files, programs generally use one of four methods:

  • Text files

  • Random-access files

  • Untyped files

  • Database files

Storing Data in Text Files

A text file, sometimes called an ASCII or plain text file, contains nothing but characters, such as letters, numbers, and symbols.

Text files only store actual data, such as names and addresses, but don't contain any formatting information, such as fonts or underlining. Because text files only contain data, they represent a universal file format that any computer, from an ancient Commodore-64 to a Cray super computer, can read and use.

Text files typically store data as one long string of data like this:

Joe Smith 123 Main Street New York NY 10012

Get Beginning Programming ALL-IN-ONE DESK REFERENCE FOR DUMMIES® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.