#include <iostream >bool good() const;
The good( ) function is a member of ios.
The
good( )
function returns
true
if no I/O errors have occurred in the associated stream;
Related functions are bad( ) , fail( ) , eof( ) , clear( ) , and rdstate( ) .
#include <iostream >istream &ignore(streamsize num = 1, int delim = EOF);
The ignore( ) function is a member of istream.
You can use the
ignore( )
member function to read and discard characters from the input stream. It reads and discards
Related functions are get( ) and getline( ) .
#include <fstream>void fstream::open(const char * filename , ios::openmode mode = ios::in ios:: out); voidifstream ::open(const char * filename , ios::openmode mode = ios::in); voidofstream ::open(const char * filename , ios::openmode mode = ios:: out ios::trunc);
The open( ) function is a member of fstream , ifstream , and ofstream.
A file is associated with a stream by using the
open( )
function. Here,
filename
is the
ios::app
ios::ate
ios::binary
ios::in
ios::out
ios::trunc
You can combine two or more of these values by ORing them together.
Including
ios::app
causes all output to that file to be appended to the end. This value can be used only with files capable of output. Including
ios::ate
causes a seek to the end of the file to occur when the file is opened. Although
ios::ate
causes a seek to the end of file, I/O operations can still occur
The ios::binary value causes the file to be opened for binary I/O operations. By default, files are opened in text mode.
The ios::in value specifies that the file is capable of input. The ios::out value specifies that the file is capable of output. However, creating an ifstream stream implies input, creating an ofstream stream implies output, and opening a file using fstream implies both input and output.
The
ios::trunc
value causes the contents of a
In all cases, if open( ) fails, the stream will be false . Therefore, before using a file, you should test to make sure that the open operation succeeded.
Related functions are close( ) , fstream( ) , ifstream( ) , and ofstream( ) .
#include <iostream >int peek();
The peek( ) function is a member of istream .
The
peek( )
function returns the
A
#include <iostream >streamsize precision() const;streamsize precision(streamsize p );
The precision( ) function is a member of ios (inherited from ios_base ).
By default, six digits of precision are displayed when floating-point values are output. However, using the second form of precision( ) , you can set this number to the value specified in p. The original value is returned.
The first version of precision( ) returns the current value.
Related functions are width( ) and fill( ) .
| Programming Tip |
There are two flavors of manipulators: those without parameters and those with parameters. While the creation of parameterized manipulators is beyond the scope of this book, it is quite easy to create your own parameterless manipulators. All parameterless output manipulators have this skeleton:
{ // your code here return stream; }
Here, manip-nameis the name of the
All parameterless input manipulators have this skeleton: istream & manip-name (istream & stream ) { // your code here return stream; } An input manipulator receives a reference to the stream for which it was invoked. This stream must be returned by the manipulator. Here is an example of a simple output manipulator called setup( ). It turns on left-justification, sets the field width to 10, and specifies the dollar sign as the fill character. |
#include <iostream> #include <iomanip> using namespace std; ostream &setup(ostream &stream) { stream.setf(ios::left); stream << setw(10) << setfill('$'); return stream; } int main() { cout << 10 << " " << setup << 10; return 0; }
Remember: Your manipulator must return stream. If it doesn’t, the manipulator cannot be used in a series of input or output operations.