Flylib.com

Books Software

 
 
 

C Programming on the IBM PC (C Programmers Reference Guide Series) - page 411

good

#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; otherwise , it returns false .

Related functions are bad( ) , fail( ) , eof( ) , clear( ) , and rdstate( ) .


ignore

#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 characters until either num characters have been ignored (1 by default) or until the character specified by delim is encountered ( EOF by default). If the delimiting character is encountered, it is removed from the input stream. The function returns a reference to the stream.

Related functions are get( ) and getline( ) .


open

#include <fstream>void fstream::open(const char *

filename

, ios::

openmode


mode =

ios::in  ios:: out); void

ifstream

::open(const char *

filename

, ios::openmode

mode =

ios::in); void

ofstream

::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 name of the file, which may include a path specifier . The value of mode determines how the file is opened. It must be one (or more) of these values:

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 anywhere within the file.

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 preexisting file by the same name to be destroyed and the file is truncated to zero length.

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( ) .


peek

#include <

iostream

>int peek();

The peek( ) function is a member of istream .

The peek( ) function returns the next character in the stream or EOF if the end of the file is encountered . It does not, under any circumstances, remove the character from the stream.

A related function is get( ) .


precision

#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:

ostream & manip- name (ostream & stream )

{

// your code here

return stream;

}

Here, manip-nameis the name of the manipulator . Notice that a reference to a stream of type ostream is returned. This is necessary if a manipulator is to be used as part of a larger I/O expression. It is important to understand that even though the manipulator has as its single argument a reference to the stream upon which it is operating, no argument is used when the manipulator is inserted in an output operation.

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.