Used to check in one operation or set multiple flags.
“scratchpad” file to the output mode to open and empty. The name “precious” files, then
For some reason, the standard library types do not allow copy or assignment operation. The reason behind the third
in each IO object manages a buffer that stores the program to read and write data. If the following statement:
while (cin>> word)
contents of the buffer is flushed, that is written to the real output device or file:
In addition, the standard library also defines a group of related types, support for wchar_t type. Each class with “w” 382
pieces to clear the stream before the call to clear the state.
8.12:
to use both ways: to achieve some processing on each row, while the other will have to manipulate the process in each row each
cout <<"hi!" < a value in writing before the refresh.
write is the object of the string object. The class also defines a member named str is used to read
8.6:
out to open the file do write, delete the existing data file
s.bad () If you set stream s, badbit value, then the function returns true
ifstream input (it-> c_str ());// open the file;
string stream object to read and write. In addition to the ordinary iostream operations, it also defines a named
not specified in mode, the file will empty the existing data. If you open the file specified trunc
<<"val2:" < outfile.open (“scratchpad”, ofstream:: out);
write function opens the file for input, reads the contents of the file string class
? ostream (output stream) type, to provide output operations.
while (it! = files.end ()) {
1. ifstream, derived from the istream to provide reading files.
member function call open an existing file fstream object with a specific binding. In order to achieve
show form, or vice versa. sstream input and output operations to the arithmetic types can be automatically converted to the corresponding
IO object can not be copied or assignment
and open the code defines a pair of fstream object. infile is read the stream, and outfile
if (outfile) / / ok to use outfile?
8.3. output buffer management
set failbit.
a ostream object can only tied together with an istream object. If you call
os <<"Goodbye!" < achieve IO inheritance is the source of errors. Some errors are recoverable; some mistakes are made
because ifstream inherits istream, so it can ifstream on
cout < default, the stream object associated with the ifstream file will open in mode, this mode allows text
8.4.2. file mode
input.open (it-> c_str ()); / / open the file
object. These two objects are not tied specific file. Using fstream object, you must also
C standard library class to handle input and output:
2. Ofstream, derived from the ostream to provide written documentation of the function.
order, usually according to the final output error to find the program area. If a crash occurs
infile.open (“in “);// open file named” in “in the current directory
s.rdstate () Returns the stream s, current conditions, the return value type strm: : iostate
in.close (); / / close in case it was already open
in the back of a particular output statement, then we can see this location in the program after the error.
/ / ok to use cin, it is in a valid state
book 2010-09-28 11:12:08 Comments 0 read 1 Word Count: C input / output (input / output ) provided by the standard library. Standard library defines a family of types,
the object is the failbit and badbit bit open simultaneously. Arguments:
If the programmer needs to re-file stream to read and write multiple files, you must read another article
pieces. Constructor initializes type using the arrow operator (Section 5.6) the solution of it for reference,
381
prefix, as with the char type versions separate. Thus, wostream, wistream and
8.13:
iostream standard library support for memory input / output, as long as the flow and storage in the program memory
input_istring>> dump>> val1>> dump>> val2;
cin.clear (istream:: failbit); / / reset the stream
control bit, if its value is 1, the operation of the two operands at least one corresponding to the binary bit is 1.
is.badbitis.failbit
? getline function, were required to take istream type and string type, two references
inherited form of type iostream. These types can be used to read and write files or string object.
cout <<"hi!" < 8.4. file input and output
to get it, said the current members of the string object c_str . Open the file by the constructor,
rewrite the above procedures, each word in the document is stored as a
container used here. members get str created before ostringstream object with associated
efficiency, and return to the stream.
international character support does not automatically refresh when the program crashes buffer, which may happen: the program to do
while (cin>> ival,! cin.eof () ) {
default values.
If you ignore the clear call, the loop can only read the first file. To see why, on
which specific error encountered. Table 8.2 lists the standard library functions and defines a set of tags, providing access
standard library defines a number of container classes and a range of generic algorithms, so that programmers can be more concise, abstract and use
iostream type in the same stream on the input and output operations. Standard Library also defines two other
in.open (file.c_str ()); / / open the file we were given
error, then the flow is usually no longer continue to use. If there is a recoverable error, such as the wish to be
then the fstream object is not ready for the IO. When the test object
8.3 lists all the file mode.
The string operator can read the input line of each word. 400
type of vector container, the container object to store each line of one yuan
As istringstream type ifstream and istream classes inherited, so the two known types of string
strings as files name. Fstream object is created, if the call is passed to the
like istream reference parameter to a function. Section 8.2 using the first
asserted state. And then try to open a given file. If the open fails, the state will mark the flow conditions
istream:: iostate old_state = cin.rdstate ();
designers can easily use the standard IO library facilities to read and write custom class objects. Class type usually makes
flow is wistringstream, wostringstream and wstringstream. Standard Library also defines
terminator, terminator corresponding to the fact that the file is another file has nothing to do with the other documents.
read before output.
first calculate each of its operands and returns the right operand as a result of the operation. Therefore,
for open_file function, please explain if you close the program execution functions
s.clear (flag) to the stream s status is set to a specified condition is valid. the type of flag is
string object can be tied up. At this point, you can use iostream input and output operators read this
Exercises Section 8.4.1
each IO header file defines the char and wchar_t types of classes and standard input / output
/ / append mode; adds new data at end of existing file named “file2″
This cycle constantly read cin, until you reach the end of file character or an unrecoverable read error
finish line after a write operation flushed stream:
process (s);
write a function that reads the exercises named file.
8.2:
just call the open function, we must set the file mode, the mode settings can
the Illustration. The top circle represents the base class (or “parent”), the base class and derived class (or “sub
string type of object
copyOut file in both input and output mode on: < br /> ostream print (ostream os);
while (it! = files.end ()) {
strm.str () returns strm type of object stored in the string
/ / construct an ifstream and bind it to the file named ifile
? < windows support files and control devices such as read-write (IO). also defines a number of other types, so that string
while (input>> s) / / do the work on this file
you e already seen a word or a time line of approach for each type of program The first one-way
nounitbuf manipulator will flow back to normal use, flush the buffer by the system management mode.
This requirement has two particularly important implications, as seen in Chapter IX The only support replication of meta-type version of the distinction between
: wchar_t types of standard input object is wcin; standard output is wcout; and standard
following statement is wrong, point out the error and correct it: < br /> reads a word stored in the form of reading the line.
read istringstream object automatically numeric representation of the character data converted to the corresponding count
with this condition is reached before the test cin end of the file or some other error conditions encountered class
C provides for efficient programming using abstract way. standard library is a good example:
out, trunc and app mode can only be used specified ofstream or fstream object associated with the
is equivalent to:
ofstream appfile (“file2″, ofstream:: app);
open mode all of the above combination can also add ate mode of These models will add ate values ??for stream type
differ. In addition, the model can explicitly open the file.
Table 8.3 lists the valid combinations of open modes are inherited
the type of relationship share the same interface. Following Cheng Qiji class derived class property.
Chapter XV of the standard IO library with built-in types defined for the operator and the rules to read and write.
level seems quite terrible. To manage this complexity, the standard library uses inheritance (inheritance)
way to pass the stream object to read and write to the IO object will change its state, so reference must be non-
to define a set of object-oriented (object-oriented) class in the fourth part of the book will be written in more detail
open_file similar program to open the file for output.
outtrunc out the same pattern with
istringstream object to call. Similarly, ostream