This preview shows page 1-2-3-4-5-6-7-52-53-54-55-56-57-58-59-104-105-106-107-108-109-110 out of 110 pages.
Chapter 6Overview6.1 Streams and Basic File I/OI/O StreamsObjectsStreams and Basic File I/Ocin And cout StreamsWhy Use Files?File I/OStream VariablesStreams and AssignmentDeclaring An Input-file Stream VariableDeclaring An Output-file Stream VariableConnecting To A FileUsing The Input StreamUsing The Output StreamExternal File NamesClosing a FileSlide 19Slide 20Member FunctionsObjects and Member Function NamesClassesClass Member FunctionsCalling a Member FunctionMember Function Calling SyntaxErrors On Opening FilesCatching Stream ErrorsHalting ExecutionUsing fail and exitTechniques for File I/OAppending Data (optional)Slide 33File Names as Input (optional)Using A Character StringSlide 36Slide 376.2 Tools for Streams I/OTools for Stream I/OFormatting Output to Filesout_stream.precision(2);setf(ios::fixed);setf(ios::showpoint);Slide 44Creating Space in OutputNot Enough Width?Unsetting FlagsManipulatorsThe setw ManipulatorThe setprecision ManipulatorManipulator DefinitionsStream Names as ArgumentsThe End of The FileEnd of File ExampleStream Arguments and NamespacesProgram ExampleSlide 57Slide 58Slide 596.3 Character I/OCharacter I/OLow Level Character I/OMember Function getUsing getget SyntaxMore About getThe End of The Line'n ' vs "n "Member Function putput SyntaxMember Function putbackputback ExampleProgram Example Checking InputChecking Input: get_intChecking Input: new_lineSlide 76Slide 77Checking Input: Check for Yes or No?Mixing cin >> and cin.get'n' ExampleA Fix To Remove 'n'Another 'n' FixDetecting the End of a FileUsing eofThe End Of File CharacterHow To Test End of FileProgram Example: Editing a Text FileSlide 88Slide 89Character FunctionsThe toupper Functiontoupper Returns An intThe isspace FunctionSlide 94Slide 956.4 InheritanceInheritanceInheritance and StreamsStream Parameters Reviewtwo_sum Is Not VersatileFixing two_sumDerived Classes and ParametersDerived Class ArgumentsInheritance RelationshipsInheritance and OutputProgram Example: Another new_line FunctionProgram Example: Calling new_lineDefault ArgumentsMultiple Default ArgumentsDefault Argument ExampleChapter 6I/O Streams as an Introduction to Objects and ClassesSlide 6- 2Overview6.1 Streams and Basic File I/O 6.2 Tools for Stream I/O6.3 Character I/O6.4 Inheritance6.1 Streams and Basic File I/OSlide 6- 4I/O StreamsI/O refers to program input and output–Input is delivered to your program via a stream object–Input can be fromThe keyboardA file–Output is delivered to the output device via a streamobject–Output can be to The screenA fileSlide 6- 5ObjectsObjects are special variables that–Have their own special-purpose functions–Set C++ apart from earlier programming languagesSlide 6- 6Streams and Basic File I/OFiles for I/O are the same type of files used tostore programsA stream is a flow of data.–Input stream: Data flows into the programIf input stream flows from keyboard, the program willaccept data from the keyboardIf input stream flows from a file, the program will acceptdata from the file–Output stream: Data flows out of the programTo the screenTo a fileSlide 6- 7cin And cout Streamscin–Input stream connected to the keyboardcout –Output stream connected to the screencin and cout defined in the iostream library–Use include directive: #include <iostream>You can declare your own streams to use with files.Slide 6- 8Why Use Files?Files allow you to store data permanently!Data output to a file lasts after the program endsAn input file can be used over and over–No typing of data again and again for testingCreate a data file or read an output file at yourconvenienceFiles allow you to deal with larger data setsSlide 6- 9File I/OReading from a file–Taking input from a file–Done from beginning to the end (for now)No backing up to read something again (OK to start over)Just as done from the keyboardWriting to a file–Sending output to a file–Done from beginning to end (for now)No backing up to write something again( OK to start over)Just as done to the screenSlide 6- 10Stream VariablesLike other variables, a stream variable… –Must be declared before it can be used–Must be initialized before it contains valid dataInitializing a stream means connecting it to a fileThe value of the stream variable can be thought of as the file it is connected to–Can have its value changedChanging a stream value means disconnecting from one file and connecting to anotherSlide 6- 11Streams and Assignment A stream is a special kind of variable called an object–Objects can use special functions to complete tasksStreams use special functions instead of the assignment operator to change valuesSlide 6- 12Declaring An Input-file Stream VariableInput-file streams are of type ifstreamType ifstream is defined in the fstream library–You must use the include and using directives #include <fstream> using namespace std;Declare an input-file stream variable using ifstream in_stream;Slide 6- 13Declaring An Output-file Stream VariableOuput-file streams of are type ofstreamType ofstream is defined in the fstream library–You must use these include and using directives #include <fstream> using namespace std;Declare an input-file stream variable using ofstream out_stream;Slide 6- 14Once a stream variable is declared, connect it to a file–Connecting a stream to a file is opening the file–Use the open function of the stream object in_stream.open("infile.dat");PeriodFile name on the diskDouble quotesConnecting To A FileSlide 6- 15Using The Input StreamOnce connected to a file, the input-stream variable can be used to produce input just asyou would use cin with the extraction operator–Example: int one_number, another_number; in_stream >> one_number >> another_number;Slide 6- 16Using The Output StreamAn output-stream works similarly to the input-stream–ofstream out_stream; out_stream.open("outfile.dat"); out_stream << "one number = " << one_number << "another number = " << another_number;Slide 6- 17External File NamesAn External File Name…–Is the name for a file that the operating system usesinfile.dat and outfile.dat used in the previous examples–Is the "real", on-the-disk, name for a file –Needs to match the naming conventions on your system–Usually only used in
View Full Document