C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1

28. Saving Sequential Files to Your Computer


In This Chapter


  • Storing information in disk files

  • Opening a file

  • Using sequential files


None of the programs you’ve seen so far has been able to store data for very long. Think about this for
a moment: If you defined an integer variable, put a 14 in it, and then turned off the computer (believe
me now and try it later), that variable would no longer have 14 in it. If you turned your computer back
on and tried to find the value in the variable, you couldn’t find it—no way.


This chapter explains how to save data to your disk. When the data is on your disk, it will be there
until you change or erase it. Data on your disk is just like music on a tape. You can turn off the tape
deck, and the tape will hold the music until you change it. There’s no good reason why a user should
enter data, such as historical sales records, more than once.


Note

Files are critical to computer data programs. How useful would a word processor be
without files?

Disk Files


Disks hold data in files. You already understand the concept of files if you’ve saved a C program to a
disk file. Files can hold either programs or data. Your programs must be loaded from disk into
memory before you can run them. You also must load data from the disk file into variables before you
can work with the data. The variables also hold data before the data goes to a disk file.


Two types of files exist: sequential-access files and random-access files. Their types determine how
you can access them. If you work with a sequential-access file, you have to read or write the file in
the order of the data. In a random-access file, you can jump around, reading and writing any place in
the file.


Tip

A sequential file is like a video tape, and a random-access file is like a DVD or Blu-
Ray. You have to watch a movie in sequence on a tape (or fast-forward through it in
order), whereas you can skip to different chapters on a DVD or a Blu-Ray.
Free download pdf