Home > Backend Development > C#.Net Tutorial > What is the function of iostream header file

What is the function of iostream header file

青灯夜游
Release: 2023-01-03 09:31:18
Original
26948 people have browsed it

The iostream header file contains methods for operating input and output streams, such as reading a file in a stream; its function is to allow beginners to have a convenient command line input and output test environment. iostream is designed to provide an extensible type-safe IO mechanism.

What is the function of iostream header file

The operating environment of this tutorial: Windows 7 system, C 17 version, Dell G3 computer.

The C language does not directly handle input and output, but handles IO through a set of types defined in the standard library. These types support IO operations that read data from and write data to the device. The device can be a file, console window, etc. There are also types that allow memory IO, that is, reading data from strings and writing data to strings.

The header file in C/C 11 defines the standard input/output stream object. Including also automatically includes , , , , and .

Objects in the header file include:

(1), narrow characters (char): cin(standard input stream (object)), cout(standard output stream (object)), cerr (standard output stream for errors (object)), clog (standard output stream for logging (object));

(2), wide characters (wchar_t), that is, wide characters: wcin (standard input stream (wide) (object)), wcout (standard output stream (wide) (object)), wcerr (standard output stream for errors (wide) (object)), wclog (standard output stream for logging (wide) (object)).

__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 istream cin, *_Ptr_cin;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream cout, *_Ptr_cout;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream cerr, *_Ptr_cerr;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 ostream clog, *_Ptr_clog;

__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 wistream wcin, *_Ptr_wcin;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 wostream wcout, *_Ptr_wcout;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 wostream wcerr, *_Ptr_wcerr;
__PURE_APPDOMAIN_GLOBAL extern _CRTDATA2 wostream wclog, *_Ptr_wclog;
Copy after login

C IO heads, templates and class ( https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp10_IO.html ):

IO library:

(1), istream (input stream) type, provides input operations;

(2), ostream (output stream) type, provides output operations;

(3), cin, an istream object, the standard input stream, used to read data from the standard input;

(4), cout, an ostream object, the standard output stream, from Standard output writes data, and the output can be redirected (“>” or “1>”) to a specified file; used to write data to standard output, usually used for the normal output content of the program.

(5), cerr, an ostream object, standard error stream, usually used to output program error information or other output content that does not belong to normal logic, written to standard error, by default, written to The data of cerr is not buffered; the error message can be sent directly to the display without waiting for the buffer or a new line break before being displayed; the output can be redirected to the specified file through the "2>" method; cerr is usually used For outputting error messages or other output content that does not belong to the normal logic of the program.

(6), clog: an ostream object, standard error stream, associated to standard error; difference from cerr: cerr and clog are both standard error streams, the difference is that cerr does not go through the buffer and outputs directly to the monitor Information, and the information in the clog will be stored in the buffer by default, and will be output only when the buffer is full or endl is encountered; by default, the data written to the clog is buffered. Clog is usually used to report program execution information and store it in a log file.

(7), >> operator, used to read input data from an istream object;

(8), << operator, used to read input data from an ostream object The object writes the output data;

(9), getline function, reads a line of data from a given istream and stores it in a given string object.

IO library types and header files: iostream defines the basic types for reading and writing streams, fstream defines the types for reading and writing named files, and sstream defines the types for reading and writing memory string objects, as shown below:

To support languages ​​that use wide characters, the standard library defines a set of types and objects to manipulate data of type wchar_t. The names of wide-character versions of types and functions begin with a w. For example, wcin, wcout, and wcerr are the wide-character versions of cin, cout, and cerr, respectively. Wide character versions of types and objects are defined in the same header file as their corresponding plain char version types.

The test code is as follows:

#include "iostream.hpp"
#include <iostream>

// reference: http://www.tutorialspoint.com/cplusplus/cpp_basic_input_output.htm
int test_iostream_cout()
{
	char str[] = "Hello C++";
	std::cout << "Value of str is : " << str << std::endl;

	return 0;
}

int test_iostream_cin()
{
	char name[50];

	std::cout << "Please enter your name: ";
	std::cin >> name;
	std::cout << "Your name is: " << name << std::endl;

	return 0;
}

int test_iostream_clog()
{
	char str[] = "Unable to read....";
	std::clog << "Error message : " << str << std::endl;

	return 0;
}

int test_iostream_cerr()
{
	char str[] = "Unable to read....";
	std::cerr << "Error message : " << str << std::endl;

	return 0;
}

// reference: https://msdn.microsoft.com/en-us/library/6xwbdak2(v=vs.80).aspx
static void TestWide()
{
	int i = 0;
	std::wcout << L"Enter a number: ";
	std::wcin >> i;
	std::wcerr << L"test for wcerr" << std::endl;
	std::wclog << L"test for wclog" << std::endl;
}

int test_iostream_w()
{
	int i = 0;
	std::cout << "Enter a number: ";
	std::cin >> i;
	std::cerr << "test for cerr" << std::endl;
	std::clog << "test for clog" << std::endl;

	TestWide();

	return 0;
}
Copy after login

Related recommendations: C Video Tutorial

The above is the detailed content of What is the function of iostream header file. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template