DOC PREVIEW
GSU CSC 2311 - ch06

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.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 110 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 StreamsI/O refers to program input and output–Input is delivered to your program via a stream object–Input can be fromThe keyboardA file–Output is delivered to the output device via a streamobject–Output can be to The screenA fileSlide 6- 5ObjectsObjects are special variables that–Have their own special-purpose functions–Set C++ apart from earlier programming languagesSlide 6- 6Streams and Basic File I/OFiles for I/O are the same type of files used tostore programsA stream is a flow of data.–Input stream: Data flows into the programIf input stream flows from keyboard, the program willaccept data from the keyboardIf input stream flows from a file, the program will acceptdata from the file–Output stream: Data flows out of the programTo the screenTo a fileSlide 6- 7cin And cout Streamscin–Input stream connected to the keyboardcout –Output stream connected to the screencin 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 endsAn input file can be used over and over–No typing of data again and again for testingCreate a data file or read an output file at yourconvenienceFiles allow you to deal with larger data setsSlide 6- 9File I/OReading 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 keyboardWriting 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 VariablesLike other variables, a stream variable… –Must be declared before it can be used–Must be initialized before it contains valid dataInitializing a stream means connecting it to a fileThe value of the stream variable can be thought of as the file it is connected to–Can have its value changedChanging 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 tasksStreams use special functions instead of the assignment operator to change valuesSlide 6- 12Declaring An Input-file Stream VariableInput-file streams are of type ifstreamType 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 VariableOuput-file streams of are type ofstreamType 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- 14Once 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 StreamOnce 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 StreamAn 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 NamesAn External File Name…–Is the name for a file that the operating system usesinfile.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

GSU CSC 2311 - ch06

Documents in this Course
ch14

ch14

65 pages

ch13

ch13

68 pages

ch10

ch10

80 pages

Load more
Download ch06
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ch06 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ch06 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?