File I/O Finished off - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

File I/O Finished off

Description:

These flags stay with the stream object ... Saving Flag Settings Example. void outputStuff ... outStream.flags(flagSettings); Restoring Default setf Settings ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 22
Provided by: ColinC98
Category:
Tags: file | finished | flags

less

Transcript and Presenter's Notes

Title: File I/O Finished off


1
File I/O Finished off
  • Colin Cherry standing in for
  • Dr. Lin

2
What happens if your input is bigger than your
character array?
  • char arr10 cin gtgt arr
  • The characters are written to memory starting at
    the spot pointed to by arr
  • Depending on whether or the memory is being used,
    different things could happen
  • Harmless
  • Could be used to execute malicious code
  • Segfault!

3
Solutions (1)
  • Use the string class!
  • Will resize automatically if too small
  • Should work for most situations

4
Solutions (2)
  • Use cin.getline(arr,size)
  • Will read in up to size-1 characters
  • Will add the c-string delimiter for you
  • Will fail if buffer overflows, but gracefully
  • Have to check cin.fail()
  • Good for situations where an agreement exists
  • Any violation is undefined

5
Overflow Demo!
  • !

6
Stream object flags
  • When a file operation fails or hits EOF, it sets
    a flag
  • Two from our example, fin.fail(), fin.eof()
  • These flags stay with the stream object
  • fin.eof() returns true, even after we close the
    current file and open a new one
  • To reset stream object, call clear()

7
So where were we?
  • Went over opening closing files
  • Using the ltlt and gtgt for input output
  • Various stream flags and options
  • Precision, width, setf
  • Manipulators
  • cout ltlt setw(6) ltlt myNumber ltlt endl

8
Saving Flag Settings
  • Flag settings stay until changed
  • Precision and setf flags can be savedand
    restored
  • Function precision() returns current settingif
    called with no arguments
  • Member function flags() provides
    similarcapability

9
Saving Flag Settings Example
  • void outputStuff(ofstream outStream) int
    precisionSetting outStream.precision() long
    flagSettings outStream.flags() outStream.setf(
    iosfixed iosshowpoint) outStream.precision
    (2)
  • outStream ltlt Do stuff here!
  • outStream.precision(precisionSetting) outStrea
    m.flags(flagSettings)

10
Restoring Default setf Settings
  • Can also restore default settings cout.setf(0,
    iosfloatfield)
  • Not necessarily the last setting!
  • Default values are implementation-dependent
  • Does not reset precision settings
  • Only setf settings

11
A function that takes a stream
  • void outputStuff(ofstream outStream)
  • But this also works
  • Void outputStuff(ostream outStream)
  • Stream always need to be passed by reference

12
Stream Hierarchies
  • Input file streams class is derived from classof
    all input streams
  • It then adds open and close member functions
  • ifstream is derived from istream
  • ofstream is derived from ostream

13
Random Access to Files
  • Sequential Access
  • Most commonly used
  • Random Access
  • Rapid access to records
  • Perhaps very large database
  • Access randomly to any part of file
  • Use fstream objects
  • input and output

14
Random Access Tools
  • Opens same as istream or ostream
  • Adds second argument
  • fstream rwStreamrwStream.open(stuff, iosin
    ios out)
  • Opens with read and write capability
  • Move about in file
  • rwStream.seekp(1000)
  • Positions put-pointer at 1000th byte
  • rwStream.seekg(1000)
  • Positions get-pointer at 1000th byte

15
Random Access Sizes
  • To move about ? must know sizes
  • sizeof() operator determines number of
    bytesrequired for an objectsizeof(s) //Where s
    is string s Hellosizeof(10)sizeof(double)si
    zeof(myObject)
  • Position put-pointer at 100th record of
    objectsrwStream.seekp(100sizeof(myObject)
    1)

16
Opening for binary
  • fstream obStream, ibStream
  • obstream.open(file, iosbinary iosout)
  • // OR
  • ibstream.open(file, iosbinaray iosin)
  • iosbinary, iosin and iosout
  • Other constants that tell C how to open files
  • Like iosapp

17
Writing bytes
  • Characters are one byte each, so youve been
    doing it all along!
  • You can write other things, though
  • Base data types (int, float)
  • Classes and structs
  • Arrays
  • Just cast to character first
  • obstream.write( (char) obj, sizeof(obj))

18
Why would I do that?
  • Say you were writing software for a video store
  • Each movie is a class with title, running time,
  • status (rented or no), and rented by field
  • Inventory is an array of classes
  • Just write inventory to file, makes it easier to
    save
  • and load before and after shut-down
  • Why not just build over a database?
  • Well, if you want to do it the easy way, sure
  • Valid example Dr. Lins load on demand hash table

19
Reading bytes
  • Same deal as before
  • ibstream.read( (char) obj, sizeof(obj))
  • Make sure your object is a pointer!
  • Tricky bit
  • You need to know the size of an object in advance
  • Solutions
  • Fixed width objects (easy, not always possible)
  • Header

20
To the example!
  • !

21
Summary
  • Streams connect to files with openoperation
  • Member function fail() checks successes
  • Stream member functions format output
  • e.g. width, setf, precision
  • Same usage for cout (screen) or files
  • Member function eof
  • Used to test for end of input file
Write a Comment
User Comments (0)
About PowerShow.com