Streams Usage ◆Once declared→use normally! int oneNumber,anotherNumber; inStream >oneNumber >anotherNumber; Output stream similar: ofstream outStream; outStream.open("outfile.txt"); outStream <<"oneNumber ="<oneNumber <anotherNumber = <anotherNumber; Sends items to output file Copyright006 Pearson Addison-Wesley.All rights reserved. 12-11
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-11 Streams Usage ¨ Once declared use normally! int oneNumber, anotherNumber; inStream >> oneNumber >> anotherNumber; ¨ Output stream similar: ofstream outStream; outStream.open("outfile.txt"); outStream << "oneNumber = " << oneNumber << " anotherNumber = " << anotherNumber; ¨ Sends items to output file
File Names ◆Programs and files Files have two names to our programs ◆External file name Also called "physical file name" ◆Like"infile.txt" Sometimes considered "real file name" Used only once in program(to open) ◆Stream name Also called "logical file name" Program uses this name for all file activity Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-12
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-12 File Names ¨ Programs and files ¨ Files have two names to our programs ¨ External file name ¨ Also called "physical file name" ¨ Like "infile.txt" ¨ Sometimes considered "real file name" ¨ Used only once in program (to open) ¨ Stream name ¨ Also called "logical file name" ¨ Program uses this name for all file activity
Closing Files Files should be closed When program completed getting input or sending output Disconnects stream from file ◆In action: inStream.close(; outStream.close(); ◆Note no arguments Files automatically close when program ends Copyright006 Pearson Addison-Wesley.All rights reserved. 12-13
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-13 Closing Files ¨ Files should be closed ¨ When program completed getting input or sending output ¨ Disconnects stream from file ¨ In action: inStream.close(); outStream.close(); ¨ Note no arguments ¨ Files automatically close when program ends
File Flush Output often "buffered" Temporarily stored before written to file ◆Written in"groups" Occasionally might need to force writing: outStream.flush(); Member function flush,for all output streams All buffered output is physically written Closing file automatically calls flush() Copyright 2006 Pearson Addison-Wesley.All rights reserved. 12-14
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-14 File Flush ¨ Output often "buffered" ¨ Temporarily stored before written to file ¨ Written in "groups" ¨ Occasionally might need to force writing: outStream.flush(); ¨ Member function flush, for all output streams ¨ All buffered output is physically written ¨ Closing file automatically calls flush()
File Example: Display 12.1 Simple File Input/Output(1 of 2) Display 12.1 Simple File Input/Output 1 //Reads three numbers from the file infile.txt,sums the numbers, 2 //and writes the sum to the file outfile.txt. 3 #include <fstream> 4 using std:ifstream; A better version of this 5 using std:ofstream; program is given in Display 12.3. 6 using std::endl; 7 int main() 8 ifstream inStream; 10 ofstream outStream; 11 inStream.open("infile.txt"); 12 outStream.open("outfile.txt"); 1 int first,second,third; 4 inStream >first >second >third; outStream <"The sum of the first 3\n" 6 <"numbers in infile.txt\n" <"is "<(first second third) 18 <endli Copyright6 Pearson Addison-Wesley.All rights reserved. 12-15
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 12-15 File Example: Display 12.1 Simple File Input/Output (1 of 2)