Chapter 6: File Input and Output

Download CD Content

Chapter 2 introduced you to screen input and output. Chapters 3, 4, and 5 made use of this practical information. In this chapter, you will learn how to get input from a flat file and send output to a flat file. This can be very useful. There are times when you will need to import data from a comma delimited text file, or perhaps you wish to write errors to a log file. All this, and more, can be accomplished with file input and output.

Importing data from a flat file is, in essence, quite similar to getting data in from the keyboard. And outputting to a file is similar to outputting data to a screen. C++ handles both in similar ways, so if you fully understood the input and output techniques in Chapter 2, then this chapter should not pose any great challenges for you.

What Is a Flat File?

A flat file is simply a file that has no structure. Thus, its name—it is flat, in that it has no structure or relationships. Data is simply put into the file with no interrelationships between entries in a file, or between files. Database files (such as Microsoft Access files or .mdb files) have a definite structure. Database files also have clearly defined relationships between entries (called records) and even between different files. However, long before there were relational databases, there where flat files. You might think that with the advent of relational databases flat files are now irrelevant. However, this is not true. There are many cases where it is more appropriate to store data in a flat file than in a complex relational database. One such situation is when writing a log of events or errors.

When you open notepad and create a text file, you are creating a flat file. Any file that simply contains ASCII (The American Standard Code for Information Interchange), pronounced AZ KEY, text is a flat file. In addition to ASCII code there is now Unicode. Unicode is simply an expanded version of ASCII, made to handle international symbols. In fact, the first 255 characters of the Unicode set are the ASCII codes. An in-depth discussion of ASCII and Unicode is beyond the scope of this book. Just remember that text files are flat files. And remember what the ASCII character codes are. Figures 6.1A and 6.1B show the entire ASCII code set. You can also find references to the ASCII code set on the Internet (search ASCII character code) and in the help files that ship with many commercial software development tools.

click to expand
Figure 6.1a: The first 126 characters of the ASCII code set.

click to expand
Figure 6.1b: The last half of the ASCII code set.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: