[C++] 2-1.6 c++ input and output

[C++] 2-1.6 c++ input and output

C++ I/O occurs in streams, which are sequences of bytes.
If the byte stream flows from a device (such as a keyboard, disk drive, network connection, etc.) to memory, this is called an input operation.
If the byte stream flows from the memory to the device (such as a display screen, printer, disk drive, network connection, etc.), this is called an output operation.

1. Standard I/O

C++ introduces standard I/O, namely iostream.
Iostream is the abbreviation of Input Output Stream, which means "input and output stream";
when input and output are needed in the program, the header file iostream needs to be included, that is, #include
It contains the object used for input and output, and the cin object represents the standard input; The cout object represents the standard output; the cerr object represents the non-buffered standard error; the clog object represents the buffered standard error;

-1.1. Example of standard output stream (cout):
cout is char output;

#include <iostream> 
 
int main( )
{
    
    
   char str[] = "jn10010537"; 
   std::cout << "csdn: " << str <<std::endl;
   return 0;
}

Note:
"<<" in the example indicates the stream insertion operator, not the left shift operator. This is the result of the operator being overloaded;
operator overloading can change the meaning of the operator;
C++ compiler according to the data type of the output variable , Select the appropriate display value.

-1.2. Examples of standard input stream (cin):
cin is char input;

#include <iostream>  //使用std::cout;std::cin;std::endl;
#include<string>    //使用std::string

int main()
{
    
    	
	//name为空字符串
	std::string name;
	std::cout << "请输入你的博客名称: ";
	std::cin >> name;
	std::cout << "你的博客名称是: " << name << std::endl;
	return 0;
}

Note: In the
example, ">>" means the stream extraction operator, not the right shift operator. This is the result of the operator being overloaded; the
stream extraction operator >> can be used multiple times in a sentence, if more input is required For data, you can use the following statement:
cin >> name >> years;
This is equivalent to the following two statements:
cin >> name;
cin >>years; The
code example is as follows:

#include <iostream>  //使用std::cout;std::cin;std::endl;
#include<string>    //使用std::string

int main()
{
    
    	
	//name为空字符串
	std::string name;
	int years;
	std::cout << "请输入你的博客名称: ";
	// 注意输入的时候用空格隔开,比如:jn10010537 3
	std::cin >> name>>years;
	std::cout << "你的博客名称是: " << name << std::endl;
	std::cout << "你的博客时间是: " << years<<"年!" << std::endl;
	return 0;

}

C++ input multiple characters, separated by a space character.

For example, the input is jn10010537 3 and the
operation is as follows:
Insert picture description here
-1.3. Example of standard error stream (cerr): The
cerr object is an instance of the iostream class.
The cerr object is attached to the standard error device, which is usually a terminal, but the cerr object is unbuffered, and every stream inserted into cerr will be output immediately.

#include <iostream>
#include <string>

int main()
{
    
    
	std::string error;
	error = "cerr对象是非缓冲的,且每个流插入到cerr都会立即输出!";
	std::cerr << "2021.03.28:\n" << error <<std::endl;
	return 0;
}

-1.4. Example of standard log stream (clog): The
clog object is attached to the standard error device, but the clog object is buffered.
That is, each stream inserted into the clog will be stored in the buffer first, and will not be output until the buffer is full or the buffer is refreshed.

#include <iostream>
#include <string>

int main()
{
    
    
	std::string log;
	log = "显示:jn10010537的日志!";
	std::clog << log <<std::endl;
	return 0;
}

Suggestion:
It is a good programming practice to use cerr stream to display error messages, and use clog stream to output other log messages.

2. File I/0

-2.1. Write a text file to the disk:

#include <iostream>
#include <fstream> //使用ofstream;

int main() {
    
    

    // 声明ofstream对象outFile
    std::ofstream outFile;

    // 打开文件
    outFile.open("jn10010537.txt");

    // 写入数据
    outFile << "contend:2021.03.28" << std::endl;

    // 关闭对象
    outFile.close();
    return 0;
}

-2.2. Read the text file from the disk:

#include <iostream>
#include <fstream>   //使用ifstream
#include <string>

int main() {
    
    
    std::string line;
    std::ifstream inFile;

    inFile.open("./jn10010537.txt");
    if (!inFile.is_open())               //检查文件是否成功打开
    {
    
    
        std::cout << "cannot open!" << std::endl;
        exit(EXIT_FAILURE);
    }

    inFile >> line;
  
    std::cout << line;
    inFile.close();
    return 0;
}

3. String I/O

Follow-up supplement;

4. Network I/O

Follow-up supplement;

Guess you like

Origin blog.csdn.net/jn10010537/article/details/115272605