Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Data File Basics.

Similar presentations


Presentation on theme: "Chapter 8 Data File Basics."— Presentation transcript:

1 Chapter 8 Data File Basics

2 Objectives Understand concepts of sequential-access and random-access files. Open and close sequential files. Write data to sequential files. Read data from sequential files. Use other sequential file techniques.

3 Why Use Data Files? Recall that RAM holds data only as long as the computer is on. Data in RAM is lost as soon as your program ends. Data must be saved to a file to prevent data loss. There is more hard drive space available than RAM space.

4 Sequential-Access A sequential-access file works like an audiocassette tape. To retrieve specific data from a sequential-access data file, you must start at the beginning of the file and search for the data or records you want while moving through the file. Sequential-access files are the most widely used data files.

5 Random-Access Files A random-access file works like an audio compact disc (CD). A random-access data file allows to you to move directly to any data in the file. Random-access files are most often used to store databases. Random-access files often occupy more disk space than sequential-access files.

6 Declaring File Streams
Getting data to and from files involves streams called file streams. When declaring a file stream, you select a stream type and a name for the stream. Use ofstream outfile; for writing. Use ifstream infile; for reading.

7 Opening a File When you open a file, a physical disk file is associated with the file stream you declared. You must provide the name of the file you want to open, and whether you need to read or write data. After the filename, you need to specify the stream operation mode. Use ios::out when creating an output file and ios::in when opening a file for input.

8 Open File Examples Example 1: writing to a file
ofstream high_scores; //stream for writing high_scores.open("SCORES.DAT", ios::out); //Create the output file. Example 2: reading from a file ifstream infile; // stream for reading infile.open("MYDATA.DAT", ios::in); // Open the file for input.

9 Closing a File After a file has been opened, you can begin writing to it or reading from it. When you complete your work with the file, you must close it. infile.close(); // Close the input file.

10 Writing Data to Files Writing data to a sequential-access data file employs the insertion operator (<<) that you use when printing data to the screen. Instead of using the output operator to direct data to the standard output device (cout), you direct the data to the file stream.

11 Reading Data From Files
Before you can read data from a file you must open the file for input using a file stream. When you open a file using ios::in, the file must already exist. Some compilers will create an empty file and give unpredictable results.

12 Reading Numeric Data When reading strictly numeric data, you can use the extraction operator (>>) as if you were getting input from the keyboard. Instead of using cin as the input stream, use your file stream name.

13 Adding Data to the End of a File
Adding data to the end of an existing file is called appending. To append data to an existing file, open the file using the ios::app stream operation mode. If the file you open for appending does not exist, the operating system creates one for you.

14 Detecting the End of a File
When reading strings from a file, the getline method returns a value of true as long as it finds valid data to read. If the end of the file is reached, the getline method will return a value of false. When reading numeric values, you must detect whether the operation has failed.

15 Detecting the End of a File
A loop like the following can be used to find the end of a file that contains strings. while(getline(infile,instring)) { cout << instring << endl; }

16 Detecting the End of a File
A loop like the following can be used to find the end of a file that contains numeric data. do { infile >> x; if(!infile.fail()) { cout << x << endl; } } while(!infile.fail());

17 Using Multiple Files Reading and writing to multiple files at the same time is possible. Simply declare a separate filestream for each file that needs to be manipulated. In this way, you could even add data to the middle of an exiting file.

18 Summary Data files allow for the storage of data prior to a program’s ending and the computer’s being turned off. In a sequential-access file, data must be written to and read from the file sequentially. In a random-access file, any record can be accessed directly.

19 Summary The first step to using a file is declaring a file stream.
After a file stream has been declared, the next step is to open the file. After data is written or read, the file must be closed. The insertion operator (<<) is used to write to a data file.

20 Summary When reading numeric data, use the extraction operator.
When reading string data or when reading from a file with both string and numeric data, read the data into string objects. Adding data to the end of an existing file is called appending.

21 Summary The getline method detects the end of the file when reading strings from a file. The fail function detects the end of a file when reading numbers. You can use more than one file at a time by declaring multiple file streams.


Download ppt "Chapter 8 Data File Basics."

Similar presentations


Ads by Google