Download presentation
Presentation is loading. Please wait.
Published byPosy Goodwin Modified over 9 years ago
1
File Operations Functions Manipulating Files
2
Review The fstream library defines two classes: ifstream, for creating connections between programs and input files; andifstream, for creating connections between programs and input files; and ofstream, for creating connections between programs and output files.ofstream, for creating connections between programs and output files. Each can be operated on in much the same way as a normal istream or ostream.
3
Problem 1 Using OCD, design and implement a function that reads the name of an input file from the user, tries to open it, and returns to the caller an ifstream guaranteed to be open, and guaranteed to be connected to a file specified by the user.
4
Preliminary Analysis 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 another chance if they enter an invalid file name...
5
Behavior Our function should display a prompt for the name of the input file. It should read the name of the input file. It should try to open an ifstream to that file. If the ifstream opens successfully, our function should return that ifstream; otherwise, it should display an error message, “loop back” and give the user another chance.
6
Objects Description Type Movement Name file name string in (kbd) fileName connection ifstream out fin prompt string local -- error msg string local --
7
Operations Description 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
8
Algorithm 1. Loop: a. Display prompt for input file name. b. Read fileName. c. Open ifstream connection named fin to fileName. d. If fin opened successfully, return fin. e. Display error message End loop.
9
Discussion We have seen how to open an ifstream to a file, verify that the open succeeded, etc. We can have a function return an ifstream by making its return-type ifstream. For functions that need to receive an ifstream or ofstream, these types can be used to define function parameters.
10
Discussion (Ct’d) If we wish to avoid declaring fin within the loop, we need a way to open fin at a point different from its declaration. The ifstream (and ofstream) classes provide the function member to open a stream at a point other than its declaration. The ifstream (and ofstream) classes provide the function member open() to open a stream at a point other than its declaration.
11
Coding /* 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; }
12
Discussion In addition to the file operations we have seen, the iostream (and fstream) libraries provide a variety of additional operations for manipulating streams.
13
Status Operations To determine the status of a stream, the libraries provide these function members: –good() // returns true iff stream is ok –bad() // returns true iff stream is not ok –fail() // returns true iff last operation failed –eof() // returns true iff last file-read failed
14
Change-State Operations To change the state of a stream, the libraries provide these function members: –clear() // reset status to good –setstate(b) // set state bit b (one of ios_base::goodbit,ios_base::badbit, ios_base::failbit, or ios_base::eofbit).
15
Read-Position Operations To manipulate the read-position within an ifstream, the libraries provide these: –tellg() // returns offset of current read-position from beginning of file –seekg(offset, base) // move read-position offset bytes from base offset bytes from base (one of ios_base::beg, ios_base::cur, or ios_base::end)
16
Write-Position Operations To manipulate the write-position within an ofstream, the libraries provide these: –tellp() // returns offset of current write-position from beginning of file –seekp(offset, base) // move write-position offset bytes from base offset bytes from base (one of ios_base::beg, ios_base::cur, or ios_base::end)
17
Other Operations To look at the next character in an ifstream without advancing the read-position (i.e., without reading it), the libraries provide: –peek() // returns next char in the stream without reading it To “unread” the last char that was read, the libraries provide: –unget() // unread char most recently read
18
Another Operation To skip a given number of chars in the stream (or until a particular char is encountered), the libraries provide: –ignore(n, stopChar) // skip past n chars, or until stopChar is encountered
19
Discussion This is by no means an exhaustive list, but it does give some of the most commonly-used stream function members. See Chapter 21 of “The C++ Programming Language” by Bjarne Stroustrup (Addison- Wesley) for a complete list.
20
Summary The C++ iostream library provides a rich set of I/O functions that let a programmer: –open and close streams. –read-from/write-to streams. –get/set the state of a stream. –get the read/write position of a stream. –move the read/write position of a stream. –peek at, or unget chars from a stream. –skip over chars in a stream.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.