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 <"theNumber is"<<theNumber; Writes value to stream,which goes to file Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-6
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-6 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 << "theNumber is " << theNumber; ¨ Writes value to stream, which goes to file
Files ◆We''ll use text files ◆Reading from file When program takes input ◆Writing to file When program sends output Start at beginning of file to end Other methods available We'll discuss this simple text file access here Copyright006 Pearson Addison-Wesley.All rights reserved. 12-7
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-7 Files ¨ We’ll use text files ¨ Reading from file ¨ When program takes input ¨ Writing to file ¨ When program sends output ¨ Start at beginning of file to end ¨ Other methods available ¨ We’ll discuss this simple text file access here
File Connection Must first connect file to stream object ◆For input: ◆File→ifstream object ◆For output: ◆File→ofstream object Classes ifstream and ofstream Defined in library <fstream> Named in std namespace Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-8
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-8 File Connection ¨ Must first connect file to stream object ¨ For input: ¨ File ifstream object ¨ For output: ¨ File ofstream object ¨ Classes ifstream and ofstream ¨ Defined in library <fstream> ¨ Named in std namespace
File 1/O 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; Copyright006 Pearson Addison-Wesley.All rights reserved. 12-9
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-9 File I/O 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;
Declaring Streams Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; Must then "connect"to file: inStream.open("infile.txt"); Called "opening the file" Uses member function open Can specify complete pathname Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-10
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-10 Declaring Streams ¨ Stream must be declared like any other class variable: ifstream inStream; ofstream outStream; ¨ Must then "connect" to file: inStream.open("infile.txt"); ¨Called "opening the file" ¨Uses member function open ¨Can specify complete pathname