file operations functions manipulating files. review the fstream library defines two classes:...

20
File Operations File Operations Functions Manipulating Files Functions Manipulating Files

Upload: posy-goodwin

Post on 17-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

File OperationsFile Operations

Functions Manipulating FilesFunctions Manipulating Files

Page 2: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

ReviewReview

The fstream library defines two classes:The fstream library defines two classes:

• ifstreamifstream, for creating connections , for creating connections between programs and input files; andbetween programs and input files; and

• ofstreamofstream, for creating connections , for creating connections between programs and output files.between programs and output files.

Each can be operated on in much the Each can be operated on in much the same way as a normal istream or same way as a normal istream or ostream.ostream.

Page 3: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Problem 1Problem 1

Using OCD, design and implement a function Using OCD, design and implement a function that reads the name of an input file from that reads the name of an input file from the user, tries to open it, and returns to the user, tries to open it, and returns to the caller an ifstream guaranteed to be the caller an ifstream guaranteed to be open, and guaranteed to be connected to a open, and guaranteed to be connected to a file specified by the user. file specified by the user.

Page 4: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Preliminary AnalysisPreliminary Analysis

One way to guarantee both conditions:One way to guarantee both conditions:

• the ifstream is openthe ifstream is open

• the file was specified by the userthe file was specified by the user

is to use a loop that gives the user is to use a loop that gives the user another chance if they enter an invalid another chance if they enter an invalid file name...file name...

Page 5: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

BehaviorBehavior

Our function should display a prompt for Our function should display a prompt for the name of the input file. It should the name of the input file. It should read the name of the input file. It read the name of the input file. It should try to open an ifstream to that should try to open an ifstream to that file. If the ifstream opens successfully, file. If the ifstream opens successfully, our function should return that our function should return that ifstream; otherwise, it should display ifstream; otherwise, it should display an error message, “loop back” and an error message, “loop back” and give the user another chance.give the user another chance.

Page 6: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

ObjectsObjects

Description Type Movement NameDescription Type Movement Name

file name string in (kbd) fileName

connection ifstream out fin

prompt string local --

error msg string local --

Page 7: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

OperationsOperations

Description Predefined? Library? NameDescription Predefined? Library? Name

display a string yes string <<read a string yes string >>open connection yes fstream -- to a file

verify connection yes fstream is_open

repeat on failure yes built-in loop

return an ifstream yes built-in return

Page 8: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

AlgorithmAlgorithm1. Loop:1. Loop:

a. Display prompt for input file name.a. Display prompt for input file name.b. Read b. Read fileNamefileName..c. Open ifstream connection named c. Open ifstream connection named finfin to to fileNamefileName..d. If d. If finfin opened successfully, return opened successfully, return finfin..e. Display error messagee. Display error messageEnd loop.End loop.

Page 9: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

DiscussionDiscussion

We have seen how to open an ifstream to We have seen how to open an ifstream to a file, verify that the open succeeded, a file, verify that the open succeeded, etc.etc.

We can have a function return an We can have a function return an ifstream by making its return-type ifstream by making its return-type ifstreamifstream..

For functions that need to receive an For functions that need to receive an ifstream or ofstream, these types can ifstream or ofstream, these types can be used to define function parameters.be used to define function parameters.

Page 10: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Discussion (Discussion (Ct’dCt’d))

If we wish to avoid declaring fin within If we wish to avoid declaring fin within the loop, we need a way to open fin at the loop, we need a way to open fin at a point different from its declaration.a point different from its declaration.

The ifstream (and ofstream) classes The ifstream (and ofstream) classes provide the function member provide the function member open() to open a stream at a point other than to open a stream at a point other than its declaration.its declaration.

Page 11: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

CodingCoding/* GetIFStream() * ... */

ifstream GetIFStream(){ ifstream fin; // declare fin here string fileName;

for (;;) { cout << “\nEnter the name of the input file: “; cin >> fileName;

fin.open(fileName.data()); // open fin here

if (fin.is_open()) return fin; // verify it opened

cout << “\n*** Unable to open file \’” << fileName << “\’ for input!\n” << endl; }}

Page 12: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

DiscussionDiscussion

In addition to the file operations we have In addition to the file operations we have seen, the iostream (and fstream) seen, the iostream (and fstream) libraries provide a variety of additional libraries provide a variety of additional operations for manipulating streams.operations for manipulating streams.

Page 13: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Status OperationsStatus Operations

To determine the status of a stream, the To determine the status of a stream, the libraries provide these function libraries provide these function members:members:– good() // returns true iff stream is okgood() // returns true iff stream is ok

– bad() // returns true iff stream is not okbad() // returns true iff stream is not ok

– fail() // returns true iff last operation fail() // returns true iff last operation failedfailed

– eof() // returns true iff last file-read eof() // returns true iff last file-read failedfailed

Page 14: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Change-State OperationsChange-State Operations

To change the state of a stream, the To change the state of a stream, the libraries provide these function members:libraries provide these function members:– clear() // reset status to goodclear() // reset status to good

– setstate(b) // set state bit b (one ofsetstate(b) // set state bit b (one of

ios_base::goodbit,ios_base::goodbit,ios_base::badbit,ios_base::badbit,ios_base::failbit, ios_base::failbit,

ororios_base::eofbit).ios_base::eofbit).

Page 15: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Read-Position OperationsRead-Position Operations

To manipulate the read-position within an To manipulate the read-position within an ifstream, the libraries provide these:ifstream, the libraries provide these:– tellg() // returns offset of currenttellg() // returns offset of current

read-position read-position fromfrom

beginning of filebeginning of file

– seekg(seekg(offsetoffset, , basebase) // move read-position) // move read-position offsetoffset bytes bytes

from from basebase (one of (one of

ios_base::beg,ios_base::beg,ios_base::cur, orios_base::cur, orios_base::end)ios_base::end)

Page 16: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Write-Position OperationsWrite-Position Operations

To manipulate the write-position within an To manipulate the write-position within an ofstream, the libraries provide these:ofstream, the libraries provide these:– tellp() // returns offset of currenttellp() // returns offset of current

write-position write-position fromfrom

beginning of filebeginning of file

– seekp(seekp(offsetoffset, , basebase) // move write-position) // move write-position offsetoffset bytes bytes

from from basebase (one of (one of

ios_base::beg,ios_base::beg,ios_base::cur, orios_base::cur, orios_base::end)ios_base::end)

Page 17: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Other OperationsOther Operations

To look at the next character in an To look at the next character in an ifstream without advancing the read-ifstream without advancing the read-position (i.e., without reading it), the position (i.e., without reading it), the libraries provide:libraries provide:– peek() // returns next char in thepeek() // returns next char in the

stream without reading itstream without reading it

To “unread” the last char that was read, To “unread” the last char that was read, the libraries provide:the libraries provide:– unget() // unread char most recently readunget() // unread char most recently read

Page 18: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

Another OperationAnother Operation

To skip a given number of chars in the To skip a given number of chars in the stream (or until a particular char is stream (or until a particular char is encountered), the libraries provide:encountered), the libraries provide:– ignore(ignore(nn, , stopCharstopChar) // skip past ) // skip past nn chars, chars,

or until or until stopCharstopChar

is encounteredis encountered

Page 19: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

DiscussionDiscussion

This is by no means an exhaustive list, This is by no means an exhaustive list, but it does give some of the most but it does give some of the most commonly-used stream function commonly-used stream function members.members.

See Chapter 21 of “The C++ See Chapter 21 of “The C++ Programming Language” by Bjarne Programming Language” by Bjarne Stroustrup (Addison-Wesley) for a Stroustrup (Addison-Wesley) for a complete list.complete list.

Page 20: File Operations Functions Manipulating Files. Review The fstream library defines two classes: ifstream, for creating connections between programs and

SummarySummary

The C++ iostream library provides a rich set The C++ iostream library provides a rich set of I/O functions that let a programmer:of I/O functions that let a programmer:– open and close streams.open and close streams.

– read-from/write-to streams.read-from/write-to streams.

– get/set the state of a stream.get/set the state of a stream.

– get the read/write position of a stream.get the read/write position of a stream.

– move the read/write position of a stream.move the read/write position of a stream.

– peek at, or unget chars from a stream.peek at, or unget chars from a stream.

– skip over chars in a stream.skip over chars in a stream.