1 / 27

IOStream Library

Unit - 10. IOStream Library. Input / Output in C++. Unit Introduction. This unit covers different features in standard IOStream Library. Unit Objectives. After covering this unit you will understand… IOStream library File operations Buffering operations Seeking operations

yeriel
Download Presentation

IOStream Library

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. Unit - 10 IOStream Library Input / Output in C++

  2. Unit Introduction This unit covers different features in standard IOStream Library

  3. Unit Objectives After covering this unit you will understand… • IOStream library • File operations • Buffering operations • Seeking operations • Output stream formatting • IOStream manipulators • Creating manipulators

  4. IOStream Introduction • Input and Output are the basic programming functions • Without IOStream Library: • requires hundreds of lines of code for usage and manipulation • are error prone with dangling pointers, uninitialized pointers etc

  5. IOStream Library • IOStream Library helps in: • ease of coding • error handling • safer and efficient results • Deals with the following I/O functions in a safe, efficient and easier manner: • Standard Input • Standard Output • Files • Memory Blocks

  6. IOStreams Features • Provides an easier interface • Handles menial tasks like closing the file pointer, when the file is not in use etc • Error handling is automatic when I/O error occurs • Constructor handles all the initialisation • Destructor handles all the cleanup tasks like removing dangling pointers, memory cleanup etc

  7. File IOStreams • File IOStreams are simple to use: • Declare an Object • Use the file I/O functions • Destroy the object (the destructor is called automatically when the object goes out of scope)

  8. Example: File IOStreams #include <fstream.h> #include <iostream.h> void main() { const int sz = 100; // Buffer size; char buf[sz]; { ifstream in(”myfile.cpp"); // Read ofstream out(”myfile.out"); // Write int i = 1; // Line counter while(in.get(buf, sz)) { // Line input in.get(); // get() does not read ‘\n’ cout << buf << endl; // use endl for ‘\n’ // File output just like standard I/O: out << i++ << ": " << buf << endl;

  9. Example: File IOStreams (contd.) } } // Destructors close in & out ifstream in(”myfile.out"); // More convenient line input: while(in.getline(buf, sz)) { char* cp = buf; while(*cp != ':') { cp++; } cp += 2; // Past ": " cout << cp << endl; } }

  10. File Open Modes • You can control the way a file is opened by changing a default argument • These flags can be combined using a bitwise OR • The different modes are: • ios::in • ios::out • ios::app • ios::nocreate

  11. File Open Modes (contd.) • ios::noreplace • ios::trunc • ios::binary

  12. Buffering in IOStreams • You can buffer all the bytes in a file using buffering • It provides an easy and efficient solution to reading files or other I/O features

  13. Example: Buffering IOStreams #include <fstream.h> #include <iostream.h> void main() { ifstream in(“myfile.cpp”); cout << in.rdbuf(); // outputs entire file // another way to output the file while (in.get(*cout.rdbuf())) // redirects it to cout { in.ignore(); // ignore the terminator ‘\n’ } }

  14. Seeking in IOStream • You can move the stream pointer to different positions and read or write bytes • There are three seek functions: • ios::beg From beginning of stream • ios::cur Current position in stream • ios::end From end of stream

  15. Example: Seeking in IOStream #include <iostream> #include <fstream> void main() { ifstream in("Iofile.cpp"); ofstream out("Iofile.out"); out << in.rdbuf(); // Copy file in.close(); out.close(); // Open for reading and writing: ifstream in2("Iofile.out", ios::in | ios::out); ostream out2(in2.rdbuf()); cout << in2.rdbuf(); // Print whole file out2 << "Where does this end up?"; out2.seekp(0, ios::beg); // put pointer

  16. Example: Seeking in IOStream (contd.) out2 << "And what about this?"; in2.seekg(0, ios::beg); // get pointer cout << in2.rdbuf(); }

  17. Output Stream Formatting • You can format the output stream in STL, just like you can using printf in C++ • The different formatting functions are: • Internal formatting data • ios::skipws • ios::showbase • ios::showpoint • ios::uppercase • ios::showpos • ios::unitbuf

  18. Output Stream Formatting (contd.) • ios::stdio • Format fields • ios::basefield • ios::dec • ios::hex • ios::oct • ios::floatfield • ios::scientific • ios::fixed • automatic • ios::adjustfield • ios::left • ios::right

  19. Output Stream Formatting (contd.) • ios::internal • Width, fill and precision • int ios::width( ) • int ios::width(int n) • int ios::fill( ) • int ios::fill(int n) • int ios::precision( ) • int ios::precision(int n)

  20. Iostream Manipulators • Output stream formatting can be a tedious task • To make things easier to read and write, a set of manipulators are provided • The manipulators are: • showbase / noshowbase • showpos / noshowpos • uppercase / nouppercase • showpoint / noshowpoint

  21. IOStream Manipulators (contd.) • skipws / noskipws • left / right / internal • scientific / fixed • Some manipulators can have arguments, which require <iomanip.h> header • The argument supplied manipulators are: • setiosflags (fmtflags n) • resetiosflags(fmtflags n)

  22. IOStream Manipulators (contd.) • setbase(base n) • setfill(char n) • setprecision(int n) • setw(int n)

  23. Example: IOStream Manipulators #include <fstream> #include <iomanip> void main() { ofstream trc("trace.out"); int i = 47; float f = 2300114.414159; char* s = "Is there any more?"; trc << setiosflags(ios::unitbuf | ios::showbase | ios::uppercase | ios::showpos); trc << i << endl; // Default to dec trc << hex << i << endl; trc << resetiosflags(ios::uppercase) << oct << i; trc.setf(ios::left, ios::adjustfield); trc << resetiosflags(ios::showbase) << dec << setfill('0'); trc << "fill char: " << trc.fill() << endl; trc << setw(10) << i << endl;

  24. Example: IOStream Manipulators (contd.) trc << resetiosflags(ios::showpos) << setiosflags(ios::showpoint) << "prec = " << trc.precision() << endl; trc.setf(ios::scientific, ios::floatfield); trc << f << endl; trc.setf(ios::fixed, ios::floatfield); trc << f << endl; trc << setprecision(20); trc << "prec = " << trc.precision() << endl; trc << resetiosflags( ios::showpoint | ios::unitbuf); }

  25. Creating Manipulators • You can create your own manipulators • The manipulator may or may not have arguments • The declaration for endl is ostream& endl(ostream&); cout << “howdy” << endl; • the endl produces the address of that function • Applicator calls the function, passing it the ostream object as an argument

  26. Example: Creating Manipulators #include <iostream> ostream& nl(ostream& os) { return os << '\n'; } void main() { cout << "newlines" << nl << "between" << nl << "each" << nl << "word" << nl; } /* The expression os << '\n'; calls a function that returns os, which is what is returned from nl */

  27. Unit Summary In this unit you have covered … • Overview of the IOStream library • Different features of IOStream • IOStream operations & manipulators • Creating custom manipulators

More Related