FilesReadwrite - Source: Absolute C++, Savitch,...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Source: Absolute C++, Savitch, Addison-Wesley Source: Absolute C++, Savitch, Addison-Wesley Chapter 12 Streams and File I/O Source: Absolute C++, Savitch, Addison-Wesley 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 12-2 Source: Absolute C++, Savitch, Addison-Wesley Streams C/C++ views input and output as a flow of characters Input stream Flow into program Can come from keyboard/mouse input Can come from file Output stream Flow out of program Can go to screen Can go to file 12-3 Source: Absolute C++, Savitch, Addison-Wesley Streams Usage Weve used streams already in standard I/O 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 12-4 Source: Absolute C++, Savitch, Addison-Wesley Streams Usage Like cin, cout Consider: Lets say we define a function called inStream that comes from some file: int theNumber; inStream >> theNumber; Reads value from stream, assigned to theNumber Lets say we define a function call outStream that streas output to some file outStream << "theNumber is " << theNumber; Writes value to stream, which goes to file 12-5 Source: Absolute C++, Savitch, Addison-Wesley Files Well use text files for right now to make it simpler Reading from file When program takes input from a file Writing to file When program sends output to a file Sequential access. Start at beginning of file to end Other methods available Well discuss this simple text file access here 12-6 Source: Absolute C++, Savitch, Addison-Wesley File Connection Must first connect or open the file to stream object We will use the object oriented paradigm For input: File ifstream object For output: File ofstream object Classes ifstream and ofstream Defined in library <fstream> Named in std namespace 12-7 Source: Absolute C++, Savitch, Addison-Wesley File I/O Libraries Must have the right libraries To allow both file input and output in your program: #include <fstream> using namespace std; OR #include <fstream> using std::ifstream; using std::ofstream; 12-8 Source: Absolute C++, Savitch, Addison-Wesley Declaring Streams You must declear objects of the correct Stream like any other class variable: ifstream inStream; //Note: Not pointers ofstream outStream; // Note: Not pointers Must then "connect" to file: inStream.open("infile.txt"); Called "opening the file" Uses member function open Can specify complete pathname 12-9 Source: Absolute C++, Savitch, Addison-Wesley Streams Usage Once declared use normally just like cin!...
View Full Document

This note was uploaded on 04/02/2012 for the course COP 3014 taught by Professor Gaitros during the Fall '11 term at Florida State College.

Page1 / 35

FilesReadwrite - Source: Absolute C++, Savitch,...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online