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

What is the function of iostream header file

Mar 25, 2021 pm 03:45 PM
c++ iostream head File

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;

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;
}

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!

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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

C   Initialization techniques C Initialization techniques Jul 18, 2025 am 04:13 AM

There are many initialization methods in C, which are suitable for different scenarios. 1. Basic variable initialization includes assignment initialization (inta=5;), construction initialization (inta(5);) and list initialization (inta{5};), where list initialization is more stringent and recommended; 2. Class member initialization can be assigned through constructor body or member initialization list (MyClass(intval):x(val){}), which is more efficient and suitable for const and reference members. C 11 also supports direct initialization within the class; 3. Array and container initialization can be used in traditional mode or C 11's std::array and std::vector, support list initialization and improve security; 4. Default initialization

Explain RAII in C Explain RAII in C Jul 22, 2025 am 03:27 AM

RAII is an important technology used in resource management in C. Its core lies in automatically managing resources through the object life cycle. Its core idea is: resources are acquired at construction time and released at destruction, thereby avoiding leakage problems caused by manual release. For example, when there is no RAII, the file operation requires manually calling fclose. If there is an error in the middle or return in advance, you may forget to close the file; and after using RAII, such as the FileHandle class encapsulates the file operation, the destructor will be automatically called after leaving the scope to release the resource. 1.RAII is used in lock management (such as std::lock_guard), 2. Memory management (such as std::unique_ptr), 3. Database and network connection management, etc.

What is high-frequency virtual currency trading? The principles and technical implementation points of high-frequency trading What is high-frequency virtual currency trading? The principles and technical implementation points of high-frequency trading Jul 23, 2025 pm 11:57 PM

High-frequency trading is one of the most technologically-rich and capital-intensive areas in the virtual currency market. It is a competition about speed, algorithms and cutting-edge technology that ordinary market participants are hard to get involved. Understanding how it works will help us to have a deeper understanding of the complexity and specialization of the current digital asset market. For most people, it is more important to recognize and understand this phenomenon than to try it yourself.

C   bitwise operators explained C bitwise operators explained Jul 18, 2025 am 03:52 AM

The bit operator in C is used to directly operate binary bits of integers, and is suitable for systems programming, embedded development, algorithm optimization and other fields. 1. Common bit operators include bitwise and (&), bitwise or (|), bitwise XOR (^), bitwise inverse (~), and left shift (). 2. Use scenario stateful flag management, mask operation, performance optimization, and encryption/compression algorithms. 3. Notes include distinguishing bit operations from logical operations, avoiding unsafe right shifts to signed numbers, and not overuse affecting readability. It is also recommended to use macros or constants to improve code clarity, pay attention to operation order, and verify behavior through tests.

What is a destructor in C  ? What is a destructor in C ? Jul 19, 2025 am 03:15 AM

The destructor in C is a special member function that is automatically called when an object is out of scope or is explicitly deleted. Its main purpose is to clean up resources that an object may acquire during its life cycle, such as memory, file handles, or network connections. The destructor is automatically called in the following cases: when a local variable leaves scope, when a delete is called on the pointer, and when an external object containing the object is destructed. When defining the destructor, you need to add ~ before the class name, and there are no parameters and return values. If undefined, the compiler generates a default destructor, but does not handle dynamic memory releases. Notes include: Each class can only have one destructor and does not support overloading; it is recommended to set the destructor of the inherited class to virtual; the destructor of the derived class will be executed first and then automatically called.

Using std::optional in C Using std::optional in C Jul 21, 2025 am 01:52 AM

To determine whether std::optional has a value, you can use the has_value() method or directly judge in the if statement; when returning a result that may be empty, it is recommended to use std::optional to avoid null pointers and exceptions; it should not be abused, and Boolean return values or independent bool variables are more suitable in some scenarios; the initialization methods are diverse, but you need to pay attention to using reset() to clear the value, and pay attention to the life cycle and construction behavior.

Member Initialization Lists in C Member Initialization Lists in C Jul 19, 2025 am 02:03 AM

In C, the member initialization list is used to initialize member variables in the constructor, especially for const members, reference members, class members without default constructors, and performance optimization. Its syntax begins with a colon and is followed by a comma-separated initialization item. The reasons for using member initialization list include: 1. The const member variable must be assigned value at initialization; 2. The reference member must be initialized; 3. Class type members without default constructors need to explicitly call the constructor; 4. Improve the construction efficiency of class type members. In addition, the initialization order is determined by the order of members declared in the class, not the order in the initialization list, so be careful to avoid relying on uninitialized members. Common application scenarios include initialization constants, references, complex objects and parameter-transferred constructions

How to convert a string to uppercase or lowercase in C  ? How to convert a string to uppercase or lowercase in C ? Jul 19, 2025 am 01:34 AM

InC ,stringscanbeconvertedtouppercaseorlowercasebyprocessingeachcharacterusingstd::toupperorstd::tolowerfrom1.Casteachcharactertounsignedcharbeforeapplyingthefunctiontoavoidundefinedbehavior.2.Modifycharactersinplaceorcopythestringifpreservingtheori

See all articles