+ All Categories
Home > Documents > CSCI 333 Data Structures

CSCI 333 Data Structures

Date post: 20-Jan-2016
Category:
Upload: dom
View: 39 times
Download: 0 times
Share this document with a friend
Description:
CSCI 333 Data Structures. I/O with C++ 20 October, 2003. C++ Stream I/O hierarchy. fstream. ifstream. ofstream. multiple inheritance. iostream. istream. ostream. virtual base class. virtual base class. ios. ios class. Formatting Width of field, base of numbers State - PowerPoint PPT Presentation
Popular Tags:
24
CSCI 333 Data Structures I/O with C++ 20 October, 2003
Transcript
Page 1: CSCI 333 Data Structures

CSCI 333Data Structures

I/O with C++

20 October, 2003

Page 2: CSCI 333 Data Structures

C++ Stream I/O hierarchy

ios

istream ostream

iostream

ifstream

multipleinheritance

virtualbase class

virtualbase class

fstreamofstream

Page 3: CSCI 333 Data Structures

ios class

• Formatting– Width of field, base of numbers

• State– Open, closed, end-of-file, failed

• Constants– Open for read/write/append

• Buffering– Using streambuf class

Page 4: CSCI 333 Data Structures

ANSI C++ ios

• To support internationalization– ios is a template of

•basic_ios

– Specialized for character set

• But western programmers don’t worry– Except when looking at the documentation

Page 5: CSCI 333 Data Structures

ios flags for opening files

ios::in Open for input

ios::out Open for output

ios::app Append to file

ios::trunc Truncate file

ios::nocreate File if file doesn’t exist

ios::noreplace Fail if file exists

Page 6: CSCI 333 Data Structures

ios state methods

F.good() No errors

F.fail() Failure, stream usable

F.bad() Failure, stream not usable

F.eof() End-of-file

!F Failed or bad

Page 7: CSCI 333 Data Structures

ios formatting methods

F.setw(int) Sets width,

returns the stream

F.width(int) Sets width

returns current width

F.fill(char) Sets fill character

returns current fill

Page 8: CSCI 333 Data Structures

ios flags for moving in files

ios::beg Move relative to beginning of file

ios::cur Move relative to

current position

ios::end Move relative to

end of file

Page 9: CSCI 333 Data Structures

istream class

• Stream input class

• Read from files– to all sorts of variables

• Seek to arbitrary file position

• Templated in ANSI C++– from basic_istream

Page 10: CSCI 333 Data Structures

Some istream methods

F >> x Extract from stream

F.read(buff, n) Extract into buffer

F.seekg(newPos) Move get pointer

F.seekg(nP,skpDr) Move get pointer,

relative to seek direction

F.tellg() Returns get pointer

Page 11: CSCI 333 Data Structures

Some ostream methods

F << x Insert into stream

F.write(buff, n) Insert from buffer

F.seekp(newPos) Move put pointer

F.seekp(nP,skpDr) Move put pointer,

relative to seek direction

F.tellp() Returns put pointer

Page 12: CSCI 333 Data Structures

The iostream methods

• Multiple inheritence – istream– ostream

• Single ios subclass– Since ios is a virtual subclass

Page 13: CSCI 333 Data Structures

The standard streams

cin Standard input

cout Standard output

cerr Standard error

Page 14: CSCI 333 Data Structures

The fstream methods

• Most inherited from *stream methods fstream F ; fstream F(filename, flags) ;– F.open(filename, flags) ;– F.close() ;

Page 15: CSCI 333 Data Structures

// Opening and closing with ifstream and ofstream#include <fstream>using namespace std ; int main(int argc, char *argv[]) { ifstream RStream ; ofstream WStream ; RStream.open(argv[1]) ; if (RStream.bad()) cerr << "Unable to read " << argv[1] << endl ; RStream.close() ; WStream.open(argv[1]) ; if (WStream.bad()) cerr << "Unable to write " << argv[1] <<

endl ; WStream.close() ;}

Page 16: CSCI 333 Data Structures

// Opening and closing with fstream

#include <fstream>

using namespace std ;

int main(int argc, char *argv[])

{

fstream RWStream ;

RWStream.open(argv[1], ios::in) ;

if (RWStream.bad())

cerr << "Unable to read " << argv[1] << endl ;

RWStream.close() ;

RWStream.open(argv[1], ios::out) ;

if (RWStream.bad())

cerr << "Unable to write " << argv[1] << endl ;

RWStream.close() ;

}

Page 17: CSCI 333 Data Structures

// Silly file updates

#include <fstream>

using namespace std ;

int main(int argc, char *argv[])

{

fstream RWStream ;

RWStream.open(argv[1], ios::in | ios::out | ios::binary) ;

if (RWStream.bad())

cerr << "Unable to read or write" << argv[1] << endl ;

else

{

char buff[7] ;

// Read from characters 7 to 13

RWStream.seekg(7, ios::beg) ;

RWStream.read(buff, 7) ;

// Write to characters 14 to 20

RWStream.seekp(14, ios::beg) ;

RWStream.write(buff, 7) ;

RWStream.close() ;

}

}

Page 18: CSCI 333 Data Structures

// Reverse a file#include <fstream> using namespace std ;

int main(int argc, char *argv[]) { fstream RWStream ;

RWStream.open(argv[1], ios::in | ios::out | ios::binary) ; if (RWStream.bad()) cerr << "Unable to read or write" << argv[1] << endl ; else { char buffFront[1] ; // extremely inefficient char buffRear[1] ; RWStream.seekp(0, ios::end) ; int fileSize = RWStream.tellp() ; for (int i=0; i<fileSize/2; ++i) { RWStream.seekg(i, ios::beg) ; RWStream.read(buffFront, 1) ; RWStream.seekg(fileSize-i-1, ios::beg) ; RWStream.read(buffRear, 1) ; RWStream.seekp(i, ios::beg) ; RWStream.write(buffRear, 1) ; RWStream.seekp(fileSize-i-1, ios::beg) ; RWStream.write(buffFront, 1) ; } } RWStream.close() ;}

Page 19: CSCI 333 Data Structures

// writing numbers in ASCII and binary#include <fstream> using namespace std ;

int main(int argc, char *argv[]) { ofstream WStream ; WStream.open(argv[1], ios::binary) ; if (WStream.bad()) cerr << "Unable to write" << argv[1] << endl ; else { for (int i=333; i<343; ++i) WStream << i ; for (int i=333; i<343; ++i) WStream.write((char *)&i, sizeof(i)) ; } WStream.close() ;}

Page 20: CSCI 333 Data Structures

// formatted I/O with ios methods#include <iostream>using namespace std ;int main(int argc, char *argv[]) { cout.fill('0') ; for (int i=0; i<100; ++i) { cout.width(2) ; cout << i ; cout.width(4) ; cout << i*i << endl ; }}

Page 21: CSCI 333 Data Structures

// formatting with io manipulators

#include <iostream>

#include <iomanip>

using namespace std ;

int main(int argc, char *argv[])

{

cout.fill('0') ;

for (int i=0; i<100; ++i)

cout << setw(2) << i << " "

<< setw(4) << i*i << endl ;

}

Page 22: CSCI 333 Data Structures

// Reading integers or characters#include <iostream>using namespace std ;

int main(int argc, char *argv[]) { int i ; cin >> i ; if (cin.fail()) { cin.clear() ; char c ; cin >> c ; if (cin.fail()) cout << "Failure on I/O" ; else cout << "Read character '" << c << "'" << endl ; } else cout << "Read integer " << i << endl ;}

Page 23: CSCI 333 Data Structures

// Summing integers#include <iostream>using namespace std ; int main(int argc, char *argv[]) { int i ; int sum = 0 ; while(cin >> i && cin.good()) sum += i ; cout << "Total = " << sum << endl ;}

Page 24: CSCI 333 Data Structures

#include <iostream>#include <sstream>using namespace std ; #define INBUFFSIZE 4096int main(int argc, char *argv[]) { int sum = 0 ; char inLine[INBUFFSIZE+1] ; while (cin.getline(inLine, INBUFFSIZE) && cin.gcount()) { int lineSum = 0 ; int i ; istringstream lineStream(inLine) ; while (lineStream>>i && lineStream.good()) lineSum += i ; if (lineStream.eof()) sum += lineSum ; else cerr << "Ignoring bad input line: " << inLine << endl ; } cout << "Sum = " << sum << endl;}


Recommended