Input and Output Streams in 9.1 C++.
Meaning of input and output
1) Input data from keyboard and output to display screen.
2) Input and output to external files (discs).
3) Use the space specified in memory for input and output. String Input Output
Standard Output Stream
C++ Input and Output Streams
Common stream classes in I/O class libraries
Class name | Effect | header file |
---|---|---|
ios | Abstract Base Class | iostream |
istream | Universal Input Stream | iostream |
ostream | Universal Output Stream | iostream |
iostream | Universal input-output stream | iostream |
ifstream | Input File Stream | fstream |
ofstream | Output File Stream | fstream |
fstram | Input and Output File Stream | fstream |
istrstream | Input String Stream Class | strstream |
ostrstream | Output String Stream Class | strstream |
strstream | Input Output String Stream Class | strstream |
4 stream objects defined in iostream header file
object | Meaning | Corresponding device |
---|---|---|
cin | Standard input stream | keyboard |
cout | Standard Output Stream | screen |
cerr | Standard error stream | screen |
clog | Standard error stream | screen |
Format output of standard type data
1. Use Controller to Control Output Format
These controllers are defined in the header file iomanip, which needs to be declared at the beginning.
Binary: dec (ten), hex (sixteen), oct (eight)
int b = 123456; cout<<hex<<b;
Read more P50
2. Use member functions of stream objects to control output format
More See P410
Output characters with stream member function put
A member function put designed to output a single character
cout.put('a'); cout.put(97);//The character a is also output because ASCII codes are supported
put functions can be called continuously in a statement
cout.put(71).put(79).put(79).put(68).put('\n'); display GOOD Back line break
Standard input stream
cin stream
Judgement status: normal - cin value is true (non-zero value)
if(!cin) cout<<"error";
Stream member function for character input
1. Read a character using the get function
No parameters
int c; cout<<"Enter a number:"<<endl; c = cin.get(); getchar()and cin.get()Same function
Has parameters (used to determine whether the read was successful)
int ch; if(!cin.get(ch)) cout<<"File End!"<<endl;
There are three parameters (n-1 characters read from input stream)
cin.get(Character Array,Number of characters n,Termination Character) cin.get(Character Pointer,Number of characters n,Termination Character)
2. Read a line of characters with getline (n-1 characters, plus'\0'for n characters in the character array)
cin.getline(Character Array(Character Pointer),Number of characters n,Termination Character)
Other member functions
1. eof function (judge end of file)
while(!cin.eof()) Reach the end true, otherwise 0
2. peek function (observe next character)
c = cin.peek();
3. putback function
cin.putback(ch);
4. ignore function
cin.ignore(n,Termination Character); cin.ignore(5,'A');
I/O operations for 9.2 files
Opening and Closing of Files
1. Open Disk File
(1) Call member function open of file stream
ofstream outfile;//Define object outfile outfile.open("C:\new\f1.dat",ios::out);//Open file as output
(2) Specify parameters when defining stream objects
ofstream outfile("C:\new\f1.dat",ios::out);
File Input and Output Settings (see P422 for details)
mode | Effect |
---|---|
ios::in | Open file as input |
ios::out | Open the file as output (default), empty it if it already has the same name |
ios::app | Open file as output, write data added at end |
ios::ate | Open an existing file with the file pointer pointing to the end of the file |
ios::binary | Open the file binary (otherwise ASCII is defaulted) |
ios::in|ios::out | Open file as input and output, readable and writable |
ios::out|ios::binary | Open an output file in binary mode |
2. Close disk files
ofstream outfile;//Define object outfile outfile.close();
outfile can be associated with other files after closing, for example:
outfile.open("C:\new\f2.dat",ios::out);
File Read-Write Stream Operation, How to Copy Between Two Files
#include<fstream> #include<iostream> using namespace std; int main(){ char temp[20]; fstream in,out; in.open("file1.txt",ios::in); out.open("file2.txt",ios::out); while(!in.eof()){ in.getline(temp,20); cout<<temp<<endl; out<<temp<<endl; } in.close(); out.close(); return 0; }