Top Banner
Computer Programming TCP1224 Chapter 13 Sequential File Access
23

Computer Programming TCP1224 Chapter 13 Sequential File Access.

Jan 05, 2016

Download

Documents

Kathleen Paul
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Computer ProgrammingTCP1224

Chapter 13Sequential File Access

Page 2: Computer Programming TCP1224 Chapter 13 Sequential 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

Page 3: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 4: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Using Sequential Access Files

•A sequential access file is often called a text file

4

Page 5: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Using Sequential Access Files (continued)

5

Page 6: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 7: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Creating and Opening a Sequential Access File (continued)

7

Page 8: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Creating and Opening a Sequential Access File (continued)

8

Default

Conceptually similar to the >> and >

Page 9: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Creating and Opening a Sequential Access File (continued)

9

Page 10: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 11: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Determining whether a File was Opened Successfully (continued)

11

! is the Not logical operator

Page 12: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 13: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 14: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 15: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 16: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Writing Information to a Sequential Access File (continued)

16

Page 17: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 18: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 19: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Reading Information from a Sequential Access File (continued)

19

Page 20: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 21: Computer Programming TCP1224 Chapter 13 Sequential File Access.

Testing for the End of a Sequential Access File (continued)

21

Page 22: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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

Page 23: Computer Programming TCP1224 Chapter 13 Sequential File Access.

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