跳转至

4 Streams

文本统计:约 622 个字 • 22 行代码

之前 C 语言中输入输出通过 printfscanf ,而在 C++ 中引入 Streams 流这一概念用于输入输出,它的优点在于格式安全,易扩展(可重载 inserters 和 extractors)并且更加面向对象。缺点是更加冗长,可能会更慢。

什么是 Stream?是一种到设备的通用逻辑接口

  • 一维、单向的:流提供了一种处理数据的方式,这种处理方式是一维的,并且数据流动是单向的。这意味着数据只能沿着一个方向流动,例如从输入源流向程序,或者从程序流向输出目标。
  • 文件上的随机访问,但在 std::cin/cout 上不行:在处理文件时,流支持随机访问,即你可以直接跳转到文件中的任意位置进行读写操作。然而,在标准输入(std::cin)和标准输出(std::cout)上,流通常是顺序访问的,你不能随意跳转到输入或输出流的某个特定位置。

Stream naming convections

Input Output Header
Generic istream ostringstream
File ifstream ofstream
C string istrstream ostrstream
C++ string istringstream ostringstream

Stream operations

(1) Extractors

  • Read a value from the stream

  • Overload operator>>

(2) Inserters

  • Insert a value into a stream

  • Overload operator<<

(3) Manipulators

  • Change the stream state

Kinds of streams

(1) Text streams

  • Deal with ASCII text

  • Perform some characters translation 做一些字符转换操作, e.g., newline => actual OS file representation

(2) Binary streams

  • Binary data

  • No translation 不存在转换,处理二进制文件(如图像、音频文件等)时,应使用二进制流,以避免数据被错误解释或修改。


Predefined streams

  • cin: standard input
  • cout : standard output
  • cerr: unbuffered error (debugging) output
  • clog: buffered error (debugging) output

Predefined extractors and insertors

数据类型 输入格式 C I/O 格式说明符
char 字符 %c
short, int 整数 %d
long 长整型十进制整数 %ld
float 浮点数 %g
double 双精度浮点数 %lg
char[], char* 字符串 %s
void* 指针 %p

Defining a stream extractor

Has to be a 2-argument free function

  • The first argument is an istream&

  • The second argument is a reference to a value

istream& operator>>(istream& is, T& obj) {
 // specific code to read obj 
 return is;
}

Return an istream& for chaining 返回 istream& 的目的是为了链接

Creating a stream inserter

Has to be a 2-argument free function

  • The first argument is an ostream&

  • The second argument is a const reference

ostream& operator<<(ostream& os, const T& obj) {
 // specific code to write obj 
 return os; 
}

Return an ostream& for chaining


Other input operators

  1. int get()
  • Returns the next character in the stream
  • Returns EOF if no characters left

    int ch;
    while ((ch = cin.get()) != EOF)
     cout.put(ch);
    
  1. get(char *buf, int limit, char delim='\n')
  • Read up to limit characters, or to delim

  • Appends a NULL character to buf

  • Does NOT consume the delimiter

  1. getline(char *b, int l, char d='\n')
  • Similar to above

  • Does consume the delimiter

  1. int gcount()
  • Returns number of characters just read
  1. void putback(char c)
  • Pushes a single character back into the stream
  1. char peek()
  • Examines the next character without consuming it

Other output operators

  1. put(char)
  • Prints a single character
  1. flush()
  • Force output of stream contents
cout << "Enter a number";
cout.flush();

Formatting using manipulators

调用 #include <iomanip>

#include <iostream>
#include <iomanip>
int main() {
 cout << setprecision(2) << 1230.243 << endl;
 cout << setw(20) << "OK!";
 return 0;
}

输出就是这样的

1.2e+03
                OK!

评论区

对你有帮助的话请给我个赞和 star => GitHub stars
欢迎跟我探讨!!!