computer programming tcp1224 chapter 13 sequential file access
TRANSCRIPT
Computer ProgrammingTCP1224
Chapter 13Sequential File Access
Objectives
• File types• Open a sequential access file• Determine whether a file was opened
successfully• Write data to a sequential access file• Read data from a sequential access file• Test for the end of a sequential access file• Close a sequential access file
2
File Types
• A program can “read” from or “write” to a file▫Files to which information is written are
output files▫Files that are read by the computer are input
files
• Types of files in C++▫Sequential
Information is accessed in consecutive order
▫Random Can be accessed in consecutive or in random order
▫Binary Information can be accessed by its byte location
3
Using Sequential Access Files
•A sequential access file is often called a text file
4
Using Sequential Access Files (continued)
5
Creating and Opening a Sequential Access File• You must create the input and output file objects
used in a program
▫#include <fstream> For ifstream and ofstream classes (see next slide)
▫using std::ifstream; and using std::ios;
6
Creating and Opening a Sequential Access File (continued)
7
Creating and Opening a Sequential Access File (continued)
8
Default
Conceptually similar to the >> and >
Creating and Opening a Sequential Access File (continued)
9
Determining whether a File was Opened Successfully• open() may fail when attempting to open a file
▫E.g., it will not be able to create an output file when the path in fileName does not exist, or when the disk is full
10
Determining whether a File was Opened Successfully (continued)
11
! is the Not logical operator
Closing a Sequential Access File
• In most programming sequence, after a “handle” is open, it needs to be close. Handle can be file (in this case), network or other devices.
• To prevent the loss of data, close a sequential access file as soon as program finishes using it
12
Skeleton Code for File Access#include <iostream>
#include <fstream>
using namespace std;
ifstream inputFile;
ofstream outputFile;
int main()
{
inputFile.open("myinput.txt");
// This is the same as inputFile.open("myinput.txt", ios::in);
if (inputFile.is_open())
{
cout << "Input file successfully open" << endl;
inputFile.close();
}
else
cout << "Input file cannot be opened" << endl;
13
Skeleton Code for File Access (Con’t)
outputFile.open("myoutput.txt", ios::out);
if (outputFile.is_open())
{
cout << "Output file successfully open" << endl;
outputFile.close();
}
else
cout << "Output file cannot be opened" << endl;
}
14
Writing Information to a Sequential Access File• Field: single item of information about a person,
place, or thing▫E.g., a name, a salary, a SSN, or a price
• Record: a collection of one or more related fields▫Contains data about a specific person, place, or
thing▫The college you are attending keeps student
records Examples of fields include your SSN, name, address,
phone number, credits earned, and grades earned
15
Writing Information to a Sequential Access File (continued)
16
Writing Information to a Sequential Access File (continued)• To verify if information was written correctly,
open the (sequential access) file in a text editor▫E.g., the text editor in Visual C++ or Notepad
17
Reading Information from a Sequential Access File• Use >> to read char and numeric data from a
file• Use getline() to read string data from a
sequential access file▫The default delimiter character is the newline
character (‘\n’)
18
Reading Information from a Sequential Access File (continued)
19
Testing for the End of a Sequential Access File• We need to know when we have reached the end
of the file.• A file pointer keeps track of the next character
either to read from or write to a file▫When a sequential access file is opened for
input, the file pointer is positioned before the first character
▫As characters are read, the pointer is moved forward
20
Testing for the End of a Sequential Access File (continued)
21
Summary
• Sequential access files can be input or output files
• To use a text file, program must contain:▫include <fstream> directive▫using std::ios; statement
• Use the ifstream and ofstream classes to create input and output file objects, respectively
• Use is_open() to determine whether a text file was opened successfully
• Use close() to close a file▫Failing to close an open file can result in loss of
data
22
Summary (continued)
• Records in a text file are usually written on a separate line in the file▫Use endl
• eof() determines if file pointer is at end of the file
23