21
Chapter 8 Data File Basics

Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

Embed Size (px)

Citation preview

Page 1: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

Chapter 8

Data File Basics

Page 2: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 3: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 4: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 5: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 6: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 7: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 8: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

Open File ExamplesExample 1: writing to a fileofstream high_scores; //stream for writing high_scores.open("SCORES.DAT", ios::out); //Create the output file.

Example 2: reading from a fileifstream infile; // stream for readinginfile.open("MYDATA.DAT", ios::in); // Open the file for input.

Page 9: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 10: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 11: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 12: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 13: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 14: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 15: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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;

}

Page 16: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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());

Page 17: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 18: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 19: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 20: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.

Page 21: Chapter 8 Data File Basics. Objectives Understand concepts of sequential- access and random-access files. Open and close sequential files. Write data

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.