{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

FilesReadwrite - Source Absolute C Savitch Addison-Wesley...

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 Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right 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 • We’ve 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: – Let’s say we define a function called inStream that comes from some file: int theNumber; inStream >> theNumber; • Reads value from stream, assigned to theNumber – Let’s 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 • We’ll 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 – We’ll 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

{[ snackBarMessage ]}

Page1 / 35

FilesReadwrite - Source Absolute C Savitch Addison-Wesley...

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

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