tailieunhanh - Chapter 12 Streams and File I/O

Streams connect to files with open operation, Member function fail() checks successes. Stream member functions format output, ., width, setf, precision. Same usage for cout (screen) or files | Chapter 12 Streams and File I/O Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Stream Hierarchies Preview of inheritance Random Access to Files Introduction Streams Special objects Deliver program input and output File I/O Uses inheritance Not covered until chapter 14 File I/O very useful, so covered here Streams A flow of characters Input stream Flow into program Can come from keyboard Can come from file Output stream Flow out of program Can go to screen Can go to file Streams Usage We’ve used streams already cin Input stream object connected to keyboard cout Output stream object connected to screen Can define other streams To or from files Used similarly as cin, cout Streams Usage Like cin, cout Consider: Given program defines stream inStream that comes from some file: int theNumber; inStream >> theNumber; Reads value from stream, assigned to theNumber Program defines stream outStream that goes to . | Chapter 12 Streams and File I/O Learning Objectives I/O Streams File I/O Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Stream Hierarchies Preview of inheritance Random Access to Files Introduction Streams Special objects Deliver program input and output File I/O Uses inheritance Not covered until chapter 14 File I/O very useful, so covered here Streams A flow of characters Input stream Flow into program Can come from keyboard Can come from file Output stream Flow out of program Can go to screen Can go to file Streams Usage We’ve used streams already cin Input stream object connected to keyboard cout Output stream object connected to screen Can define other streams To or from files Used similarly as cin, cout Streams Usage Like cin, cout Consider: Given program defines stream inStream that comes from some file: int theNumber; inStream >> theNumber; Reads value from stream, assigned to theNumber Program defines stream outStream that goes to some file outStream Named in std namespace File I/O Libraries To allow both file input and output in your program: #include using namespace std; OR #include using std::ifstream; using std::ofstream; Declaring Streams Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; Must then "connect" to file: (""); Called "opening the file" Uses member function open Can specify complete pathname Streams Usage Once declared use normally! .