'stream' 在 C 中是什么意思?

What does 'stream' mean in C?

我正在阅读 'C Primer Plus' 中有关文件、流和键盘输入的部分。作者将流的概念与文件联系起来,定义流如下:

Conceptually, the C program deals with a stream instead of directly with a file. A stream is an idealized flow of data to which the actual input or output is mapped. That means various kinds of input with differing properties are represented by streams with more uniform properties. The process of opening a file then becomes one of associating a stream with the file, and reading and writing take place via the stream

作者加粗的句子是什么意思?文件和流之间有什么联系?

设计 C 的人想要一种统一的方式来连接不同的顺序数据源,例如文件、套接字、键盘、USB 端口、打印机或其他任何东西。

所以他们设计了一个可以应用于所有这些的界面。该接口使用所有这些接口共有的属性。

为了更容易讨论可以通过界面使用的东西,他们给这些东西起了一个通用名称,streams

使用相同接口的好处在于可以使用相同的代码从键盘或套接字读取文件。

流是处理不同数据mediums/sources的C way。这些 可以包括 say

  1. 一个文件
  2. 一个插座

等等。 stream,作为一个界面,可以帮助您忘记数据是如何在幕后管理的,并专注于预期的目标。

A stream 是一个逻辑实体,表示 filedevice,可以接受输入输出。标准 C 中的所有输入和输出函数都对数据流进行操作。流可以分为文本流和二进制

请注意,文件和流是完全不同的东西。文件只是字节序列,而流只是促进者(帮助者)。

流出现在图片中,因为所有程序都需要以许多不同的形式与其周围环境进行交互(可以是文件,可以是 I/O 设备,如显示器和键盘,可以是网络套接字等。 ).

所以流是一个接口(一个简单的“面孔”来处理一些与我们无关的微妙之处,就像我们不需要知道如何电视遥控器有效!)用于触发 input/output 数据流,from/to 任何可以 source/destination 到 input/output 数据的东西,隐藏低级OS 为了与不同设计的硬件交互,代表程序员(即,我们 - 作为程序员 - 对重新编程并不真正感兴趣)的众多方法的实现细节 每次我们创建新软件时操作系统与各种硬件交互的方式)。

例如,考虑我们的程序从键盘获取输入的方式...,这是如何发生的?这是通过一个隐藏的(对程序员隐藏的)流发生的,OS provides for every "process"(As soon as a program is run, it will be what's called a process), and the OS gives the address to the standard stream made for a process to it automatically(i.e., we won't need to write code to locate its address). This stream is commonly called the "stdin"(rooted in the C & Unix terminology), or more formally called "The Standard Input Stream". Our programs, no matter written in what language, must be able to use such standard streams made by the OS through the standard I/O libraries of that language. As an example, in the C programming language, we may scan the standard input stream by calling the function "scanf”(scanf 会自动知道我们程序的标准输入在哪里)。

但是作为另一个重要的例子,同样在 C 中,假设这次我们的程序想要将用户的输入写入“文件”... 是吗在这种情况下,只有 stdin 流的存在就足够了吗?当然不是! 这一次,我们需要使用 一对流 ,OS 标准输入已经提供了一对流来获取用户的输入,第二个,让我们的程序和文件之间进行通信!所以我们必须创建第二个流!可以通过调用 the fopen() function. (Fun Fact: In the manual, if you notice, you will see that the returned type of this function is a pointer to a structure called FILE, but that’s only a traditional “bad choice of word” 来完成一些实际指向“流”的指针!是的,C 中的 FILE 类型确实是一个流,而不是一个文件!(我明白了,疯了!)所以请记住,指针 FILE* 不指向实际文件,它指向包含该文件信息的流,包括有关用于文件 I/O 等的缓冲区的信息)

注意:我们自己创建的流(例如文件流)可以是双向,而标准流是单向。下图中的箭头也很好地说明了这一点:

也作为C++世界的一个例子给大家做个对比,你知道里面的东西都是classes而不是结构体,所以你如果你正在输出,会遇到一个叫做“cout”的对象(输出流对象),这是一个连接到输出流的对象( C 中的标准输出),并且是 class ostream 的一个实例(来自 class 层次结构 ios_base <-- ios <-- ostream)。要使用 cout 写入标准输出流,必须使用它的“<<”方法(对应于 C 中的 printf())。这一次,cout 不足以与其他事物(例如文件)交互,我们需要创建自己的流。在C++中,可以通过实例化ifstream和ofstream classes(对应C中的FILE结构)来实现,这样得到的对象基本上和C中的“FILE*”指针起着相同的作用。

希望对您有所帮助。


插图出自 linuxhint.com

我的 C 编程学位使用这个解释,如果它有帮助的话: "A Stream is an abstraction of a file that provides a consistent interface to the programmer, regardless of the actual device."

流就是一个文件 * 像函数 C FILE * fopen (const char * filename, const char * mode) 这个函数return一个文件*

文件:

包含控制流的信息的对象 标识流并包含控制它所需的信息的对象类型,包括指向其缓冲区的指针、其位置指示器和所有状态指示器。