DOC PREVIEW
IUPUI CS 265 - Input/Output Streams

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 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 18 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 18 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 18 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 18 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 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Department of Computer and Information Science, School of Science, IUPUIStream ManipulatorsIntegral Stream BaseFloating Point PrecisionField WidthOther ManipulatorsFile StreamsFiles -- fstream classFile Stream FunctionsFile Streams - SyntaxFile Processing ModesFile Protection ModesFile Streams - ExampleFile Streams - open()File Streams - close()File I/O - ExampleFile I/O – Example contd…File I/O - Files01/13/19 1Dale RobertsDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]/Output Streams, Part 2Input/Output Streams, Part 201/13/19 2Dale RobertsStream ManipulatorsStream ManipulatorsC++ provides various stream manipulators that C++ provides various stream manipulators that perform formatting tasks: perform formatting tasks: setting widths, precision, filling, flushing setting widths, precision, filling, flushing streams, streams, skipping white spaces, inserting skipping white spaces, inserting new line, new line, inserting a null character, etc.…..inserting a null character, etc.…..01/13/19 3Dale RobertsIntegral Stream BaseIntegral Stream BaseIntegers are normally represented as base 10 Integers are normally represented as base 10 values.values.To change the base in which integers are To change the base in which integers are interpreted on a stream, insert different interpreted on a stream, insert different manipulators – manipulators – hex, octhex, oct – to reset the stream to – to reset the stream to decimal insert decimal insert decdec..The base of a stream may also be changed by The base of a stream may also be changed by the stream manipulator the stream manipulator setbasesetbase, which takes , which takes one integer argument of 10, 8 or 16 to set the one integer argument of 10, 8 or 16 to set the base.base.As setbase takes an argument, it is called a As setbase takes an argument, it is called a parameterized stream manipulator – requires parameterized stream manipulator – requires the inclusion of <iomanip.h> file the inclusion of <iomanip.h> file01/13/19 4Dale RobertsFloating Point PrecisionFloating Point PrecisionControlling of the floating point precision (i.e., Controlling of the floating point precision (i.e., number of digits to the right side of the decimal number of digits to the right side of the decimal point) is done by point) is done by setprecision (manipulator) setprecision (manipulator) or or precision (member function).precision (member function).A call to these, sets the precision for all A call to these, sets the precision for all subsequent output operations until the next subsequent output operations until the next precision-setting call is made.precision-setting call is made.A call to A call to precision precision without arguments returns without arguments returns the current precision setting. the current precision setting.01/13/19 5Dale RobertsField WidthField WidthThe The ios width ios width member function sets the field member function sets the field width (i.e., the number of character positions in width (i.e., the number of character positions in which a value should be output or the number of which a value should be output or the number of characters that should be input) and returns the characters that should be input) and returns the previous width.previous width.If values processed are smaller than the field If values processed are smaller than the field width, width, fill characters fill characters are inserted as are inserted as padding.padding.A value wider than the designated width will not A value wider than the designated width will not be truncated – the full number is printed.be truncated – the full number is printed.01/13/19 6Dale RobertsOther ManipulatorsOther ManipulatorsUser-definedUser-definedStream Format State Flags (specifies the kind of Stream Format State Flags (specifies the kind of formatting to be performed during stream I/O formatting to be performed during stream I/O operations)operations)trailing zeros, justification, padding, stream trailing zeros, justification, padding, stream base, base, scientific notation, upper/lower cases, scientific notation, upper/lower cases, setting/resetting format flags, etc.setting/resetting format flags, etc.01/13/19 7Dale RobertsFile StreamsFile StreamsProvides high level support for file operations in C++Provides high level support for file operations in C++Consists of three components:Consists of three components:•fstreamfstream - allows both input and output - allows both input and output•ifstreamifstream - allows only input - allows only input•ofstreamofstream - allows only output - allows only output01/13/19 8Dale RobertsFiles -- fstream classFiles -- fstream classInherits from fstreambase and iostream classesInherits from fstreambase and iostream classesfstreambase inherits from ios classfstreambase inherits from ios classiostream – inherits from istream and ostream iostream – inherits from istream and ostream classesclassesifstream – inherits from fstreambase and ifstream – inherits from fstreambase and istream classesistream classesofstream – inherits from fstreambase and ofstream – inherits from fstreambase and ostream classesostream classes01/13/19 9Dale RobertsFile Stream FunctionsFile Stream FunctionsContains the following functions:Contains the following functions:•open()open() - opens the stream - opens the stream•close()close() - closes the stream - closes the stream•attach()attach() - attaches the stream to file descriptor - attaches the stream to file descriptor•setbuf()setbuf() - set the stream buffer - set the stream buffer•rdbuf()rdbuf() - returns the pointer to stream buffer - returns the pointer to stream buffer•str()str() - returns a pointer to the buffer array - returns a pointer to the buffer array01/13/19 10Dale RobertsFile Streams - SyntaxFile Streams - SyntaxSyntaxSyntaxfstream(); fstream(); fstream(const char *sFileName, int nMode, int fstream(const char *sFileName, int nMode, int nProt = 0664);nProt = 0664);fstream(filedesc fdif); fstream(filedesc fdif);01/13/19 11Dale RobertsFile Processing ModesFile Processing ModesnMode can be any of the following:nMode can be any of the following:ios::inios::in - input processing - input processingios::outios::out - output processing - output processingios::truncios::trunc - discard the file contents - discard the file contentsios::noc


View Full Document

IUPUI CS 265 - Input/Output Streams

Download Input/Output Streams
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 Input/Output Streams 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 Input/Output Streams 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?