File Operations - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

File Operations

Description:

One way to guarantee both conditions: the ifstream is open. the file was specified by the user ... peek at, or unget chars from a stream. skip over chars in a stream. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 17
Provided by: joela72
Category:

less

Transcript and Presenter's Notes

Title: File Operations


1
File Operations
Functions Manipulating Files
2
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.

3
  • One way to guarantee both conditions
  • the ifstream is open
  • the file was specified by the user
  • is to use a loop that gives the user another
    chance if they enter an invalid file name...

4
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.

5
Objects
  • Description Type Movement Name

prompt string local --
file name string in (kbd) fileName
connection ifstream out fin
error msg string local --
6
Operations
  • Description Predefined? Library?
    Name

display a string yes string ltlt
read a string yes string gtgt
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
7
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.

8
  • 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 open() to open a stream at a
    point other than its declaration.

9
Coding
  • / GetIFStream()
  • ...
  • /
  • ifstream GetIFStream()
  • ifstream fin // declare fin
    here
  • string fileName
  • for ()
  • cout ltlt \nEnter the name of the input file
  • cin gtgt fileName
  • fin.open(fileName.data()) // open fin
    here
  • if (fin.is_open()) return fin // verify it
    opened
  • cout ltlt \n Unable to open file \

10
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

11
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_basegoodbit,
  • ios_basebadbit,
  • ios_basefailbit, or
  • ios_baseeofbit).

12
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
  • (one of ios_basebeg,
  • ios_basecur, or
  • ios_baseend)

13
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
  • (one of ios_basebeg,
  • ios_basecur, or
  • ios_baseend)

14
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

15
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

16
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.
Write a Comment
User Comments (0)
About PowerShow.com