[918C++ Programming] 9, Input and Output Streams

Posted by jamie85 on Thu, 23 Dec 2021 15:23:12 +0100

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 nameEffectheader file
iosAbstract Base Classiostream
istreamUniversal Input Streamiostream
ostreamUniversal Output Streamiostream
iostreamUniversal input-output streamiostream
ifstreamInput File Streamfstream
ofstreamOutput File Streamfstream
fstramInput and Output File Streamfstream
istrstreamInput String Stream Classstrstream
ostrstreamOutput String Stream Classstrstream
strstreamInput Output String Stream Classstrstream

4 stream objects defined in iostream header file

objectMeaningCorresponding device
cinStandard input streamkeyboard
coutStandard Output Streamscreen
cerrStandard error streamscreen
clogStandard error streamscreen

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)

modeEffect
ios::inOpen file as input
ios::outOpen the file as output (default), empty it if it already has the same name
ios::appOpen file as output, write data added at end
ios::ateOpen an existing file with the file pointer pointing to the end of the file
ios::binaryOpen the file binary (otherwise ASCII is defaulted)
ios::in|ios::outOpen file as input and output, readable and writable
ios::out|ios::binaryOpen 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;
} 

Topics: C++ Back-end