CIS 403503 Accelerated DataFile Structures - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CIS 403503 Accelerated DataFile Structures

Description:

while (fin.get(ch)) cout.put(ch); Things to Remember. Template allows generic programming ... What is wrong with 'while(!fin.eof())? What is a template? ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 33
Provided by: yanz
Category:

less

Transcript and Presenter's Notes

Title: CIS 403503 Accelerated DataFile Structures


1
CIS 403/503Accelerated Data-File Structures
  • Lecture 8
  • C Template Output

2
Objectives
  • In this lecture, you will learn
  • Function Template
  • Class Template
  • Specialized Template
  • I/O Streams
  • File Input Output

3
Function Template
4
Function Template
  • A pattern of a possible function
  • Generic programming

5
Find Max
  • int max(int x, int y)
  • return (x gt y)?xy
  • double max (double x, double y)
  • return (x gt y)?xy

6
Template
  • template ltclass Tgt max (T x, T y)
  • return (xgty)?xy
  • Note keyword class and typename can be used
    interchangeably

7
Class Template
8
Class Template
  • Similar to function template, a class template
    may work for any type of object
  • Just as function template is not a function, a
    class template is not a class

9
Example
  • ifndef GENERICOBJECT_H
  • define GENERICOBJECT_H
  • template lttypename Objectgt
  • class GenericObject
  • public
  • explicit GenericObject(const Object
    initValObject())storedValue(initVal)
  • const Object getValue() const return
    storedValue
  • void setValue(const Object val) storedValue
    val
  • private
  • Object storedValue
  • endif

10
Use of GenericObject
  • include GenericObject.h
  • include ltiostreamgt
  • using namespace std
  • int main ( )
  • GenericObjectltintgt x1
  • GenericObjectltdoublegt x2(1)
  • x1.setValue(2)
  • x2.setVaue(x2.getValue()2)
  • cout ltlt x1.getValue() ltlt endl
  • cout ltlt x2.getValue() ltlt endl
  • return 0

11
Specialized Template
12
Multiple Template Parameters
  • The template parameter list may have more than
    one parameter
  • template ltclass Key, class PlainTextgt
  • class Cipher
  • Cipherltint, stringgt shift

13
Template Nontype Parameters
  • A non-type parameter must be a compile-time
    constant
  • template lttypename Object, int sizegt
  • class Buffer
  • Buffer ltstring, 1024gt buf

14
Default Template Parameters
  • Template parameter list can have default values
    for both type and nontype parameters
  • template ltclass Objectchar, int size1024gt
  • class Buffer
  • Bufferltintgt buf1
  • Bufferltgt buf2

15
I/O Stream
16
Four Standard Streams
  • cin
  • Standard input
  • cout
  • Standard output
  • cerr
  • Standard error
  • Clog
  • Standard log stream

17
Error States
  • Four states
  • End-of-file
  • Stream saw end-of-file
  • Fail
  • Unsuccessful operation
  • Bad
  • Invalid operation, possibly corrupted stream
  • Good
  • Non of above

18
Example
  • template ltclass Objectgt
  • void readData(istream in, Object obj)
  • string tmp
  • while (!(in gtgt obj).eof())
  • if (in.fail())
  • in.clear() //clear error state
  • in gtgt tmp
  • cerr ltlt Skipping ltlt tmp ltlt endl

19
Output
  • Format the output by using manipulators
  • cout ltlt setw(15) //set field width
  • cout ltlt left //field on left
  • cout ltlt right
  • cout ltlt setprecision(2)
  • cout ltlt scientific // double with sci-notation
  • cout ltlt fixed //double no sci-notation

20
Input
  • Istreams support a get method to get a character
    one at a time
  • istream get (char ch)
  • e.g.
  • char ch
  • if (cin.get(ch))
  • cout ltlt read ltlt ch ltlt endl
  • else
  • cout ltltread error ltlt endl

21
More Input
  • Use unget to undo a get
  • Use peek to examine the next character without
    actually read it
  • istream unget()
  • int peek()
  • If end-of-file, return EOF as an integer value

22
File Input Output
23
Files
  • ifstream for input
  • ofstream for output
  • Opena file stream with primitive strings and
    modes
  • String constant
  • Null-terminated char array
  • c_str on a string object

24
Examples
  • ifstream f1(test.data)
  • char filename1100
  • get(filename1, 100) //read a whole line or 99
    chars
  • ofstream f2(filename)
  • string filename2
  • cin gtgtgt filename2
  • ofstream f3(filename2.c_str(), iosout
    iostrunc)
  • string filename3
  • getline(cin, filename3)
  • ofstream f4(filename3.c_str(), iosoutiosapp)

25
Modes
  • iosin
  • Open file for reading
  • iosout
  • Opern file for writing
  • iosate
  • Initial file end of file
  • iosapp
  • Appen output
  • isotrunc
  • Erase exising file before write
  • iosbinary
  • Write binary file

26
Random Access
  • ifstream has a pointer known as get pointer that
    points to the next element to be read
  • ofstream has a pointer put pointer that points to
    the location where the next element has bo be
    written.
  • May re-position the current pointing position

27
tellg() and tellp()
  • Zero input parameter, return pos_type that is an
    integer data type representing the current
    position of get pointer or put pointer

28
seekg() and seekp()
  • Change the position of stream pointers
  • Both overloaded with two prototypes
  • seekg(pos_type position)
  • seekp(pos_type position)
  • Stream pointers are changed to an absolute
    position from the beginning of the file
  • seekg(pos_type position, seekdir direction)
  • seekp(pos_type position, seekdir direction)
  • Specify an offset determined by direction
  • iosbeg
  • ioscur
  • iosend

29
Example
  • Void lastChars(const string fileName, int count)
  • ifstream fin(fileName.c_str(), iosbinary)
  • if (!fin)
  • //do something
  • else if (count lt0)
  • cout ltlt invalid counter must gt 0 ltlt endl
  • fin.seekg(0, iosend)
  • int fileSize fin.tellg()
  • if (fileSize lt count)
  • count fileSize
  • fin.seekg(-count, ioscur)
  • char ch
  • while (fin.get(ch))
  • cout.put(ch)

30
Things to Remember
  • Template allows generic programming
  • Function templates are not functions
  • Class templates are not classes
  • Templates can be instantiated with multiple
    parameters
  • Templates can be instantiated with nontype
    parameters, such as int
  • Template parameters can have defaults

31
Things to Remember
  • Four standard streams are defined cin, cout,
    cerr and clog
  • ifstream is a subclass of istream
  • ofstream is a subclass of ostream
  • iostream extends both istream and ostream
  • Error handling is checking the state of the
    stream
  • Manipulators are used to control formatting
    options for output
  • The istream and ostream allow for random access
    in the stream
  • Files are closed automatically by destructors

32
Possible Quiz Questions
  • How are errors handled in the C I/O library?
  • What is wrong with while(!fin.eof())?
  • What is a template?
  • How to implement and use a function template?
  • How to implement and use a class template?
Write a Comment
User Comments (0)
About PowerShow.com