1 / 24

CSCI 333 Data Structures

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

dom
Download Presentation

CSCI 333 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 333Data Structures I/O with C++ 20 October, 2003

  2. C++ Stream I/O hierarchy fstream ifstream ofstream multiple inheritance iostream istream ostream virtual base class virtual base class ios

  3. ios class • Formatting • Width of field, base of numbers • State • Open, closed, end-of-file, failed • Constants • Open for read/write/append • Buffering • Usingstreambufclass

  4. ANSI C++ios • To support internationalization • iosis a template of • basic_ios • Specialized for character set • But western programmers don’t worry • Except when looking at the documentation

  5. ios flags for opening files

  6. ios state methods

  7. ios formatting methods

  8. iosflags for moving in files

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

  10. Some istream methods

  11. Some ostream methods

  12. The iostream methods • Multiple inheritence • istream • ostream • Single ios subclass • Since ios is a virtual subclass

  13. The standard streams

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

  15. // 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() ; }

  16. // 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() ; }

  17. // 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() ; } }

  18. // 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() ; }

  19. // 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() ; }

  20. // 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 ; } }

  21. // 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 ; }

  22. // 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 ; }

  23. // 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 ; }

  24. #include <iostream> #include <sstream> using namespace std ; #define INBUFFSIZE 4096 int 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; }

More Related