Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize IO flow in C++

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Xiaobian to share with you how to achieve IO flow in C++, I believe most people still do not know how to, so share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Input and Output of C Language

The most common input and output methods we use in C are scanf () and printf().

scanf(): reads data from a standard input device (keyboard) and stores values in variables.

printf(): Outputs the specified text/string to a standard output device (screen).

C language with the help of the corresponding buffer to input and output. As shown below:

Among them, the function of input and output buffer: it can shield the implementation of low-level I/O, which depends on the implementation of the kernel of the operating system itself, so if this part of the difference can be shielded, it is easy to write portable programs.

You can use the contents of this section to implement the behavior of "line" reading. For computers, there is no concept of "line," so you can define the concept of "line," and then parse the contents of the buffer and return a "line."

C++ is a C++ language.

Flow in C++ is an abstract description of ordered, continuous and directional data.

C++ flow refers to the process of information input from an external input device (such as a keyboard) to the computer's internal (such as memory) and output from memory to an external output device (display).

The characteristics are: orderly continuity, with directionality.

To achieve this flow, C++ defines I/O standard class libraries, each of which is called a flow/stream class.

C++IO stream

The C++ system implements a huge class library, where ios is the base class, and other classes are directly or indirectly derived from the ios class.

1.C++ standard IO stream

The C++ standard library provides four global flow objects cin, cout, cerr, and log.

cout performs standard output, i.e. data flows from memory to console (monitor).

Cin performs standard input, that is, data is entered into the program via the keyboard.

Cerr is used for standard error output.

log output,

As can be seen from the above figure, cout, cerr, and log are three different objects of the ostream class, so there is basically no difference between these three objects now, but the application scenarios are different. It must include files and introduce the std standard namespace when used.

In addition:

(1) Cin is buffer flow. The keyboard input data is stored in the buffer, and when it is to be extracted, it is taken from the buffer. New data is requested only after the input buffer has been emptied.

(2) The input data type must be consistent with the data type to be extracted, otherwise an error occurs.

(3) Both spaces and carriage returns can be used as separators between data, so multiple data can be entered in one line or in branches. However, if it is a character type and a character string, spaces (ASCII code is 32) cannot be entered with cin, and there cannot be spaces in the character string. Carriage returns cannot be read.

(4) cin and cout can directly input and output built-in type data, and the standard library has overloaded all built-in type inputs and outputs:

(5) Custom types need to be overloaded.

2. C++ file IO stream

C++ is divided into binary files and text files according to the data format of the file content.

General steps to manipulate files with file stream objects:

(1) Define a file stream object [ifstream ifile(input only), ofstream ofile(output only), fstream iofile(both input and output)]

(2) Open a disk file using the member functions of the File Stream object

(3) Read and write files using extract and insert operators, or read and write files using member functions

(4) Close the file

IV. Stringstream

In C, if you want to convert the data of an integer variable to string format, you can use the itoa() function or the sprintf() function.

However, in the conversion, you must first give the space to save the results, the space is not well defined, and the conversion format does not match, you may also get wrong results or even program crash.

int main(){ int n = 123456789; char s1[32]; _itoa(n, s1, 10); char s2[32]; sprintf(s2, "%d", n); char s3[32]; sprintf(s3, "%f", n); return 0;}

In C++, you can circumvent this problem by using stringstream class objects.

Include the header file stream when using. Under this header file, the standard library has three classes: istringstream, ostringstream, and stringstream, which are used for stream input, output, and input/output operations, respectively.

Stringstream can be used to format numeric data into strings, string concatenation.

Stringstream actually maintains an object of type string at the bottom to hold the results.

When converting multiple data types, you must use clear() to clear them in order to convert correctly, but clear() will not clear the string object at the bottom of the stringstream.

You can use s. The str("") method sets the underlying string object to an empty string.

You can use s.str() to return stringstream to its underlying string object.

Stringstream uses string-like objects instead of character arrays to avoid the danger of buffer overflows, and it deduces parameter types without formatting controls or the risk of formatting failures.

The above is "C++ IO stream how to achieve" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report