C++:什么是流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12145357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: What is a stream
提问by Mohamed Ahmed Nabil
I have been hearing about stream, more specifically file streams.
我一直在听说流,更具体地说是文件流。
So what are they?
那么它们是什么?
Is it something that has a location in the memory?
它是否在内存中占有一席之地?
Is it something that contains data?
它是包含数据的东西吗?
Is it just a connection between a file and an object?
它只是文件和对象之间的连接吗?
Any help would be appreciated
任何帮助,将不胜感激
采纳答案by Jonathan Wood
The term stream is an abstraction of a construct that allows you to send or receive an unknown number of bytes. The metaphor is a stream of water. You take the data as it comes, or send it as needed. Contrast this to an array, for example, which has a fixed, known length.
术语流是一种构造的抽象,它允许您发送或接收未知数量的字节。比喻是水流。您可以随时获取数据,或根据需要发送数据。例如,将其与具有固定已知长度的数组进行对比。
Examples where streams are used include reading and writing to files, receiving or sending data across an external connection. However the term streamis generic and says nothing about the specific implementation.
使用流的示例包括读取和写入文件、通过外部连接接收或发送数据。然而,术语流是通用的,并没有说明具体的实现。
回答by Dietmar Kühl
IOStreams are a front-end interface (std::istream
, std::ostream
) used to define input and output functions. The streams also store formatting options, e.g., the base to use for integer output and hold a std::locale
object for all kind of customization. Their most important component is a pointer to a std::streambuf
which defines how to access a sequence of characters, e.g., a file, a string, an area on the screen, etc. Specifically for files and strings special stream buffers are provided and classes derived from the stream base classes are provided for easier creation. Describing the entire facilities of the IOStreams library can pretty much fill an entire book: In C++ 2003 about half the library section was devoted to stream related functionality.
IOStreams 是一个前端接口(std::istream
, std::ostream
),用于定义输入和输出函数。流还存储格式化选项,例如,用于整数输出的基数并std::locale
为所有类型的定制保存一个对象。它们最重要的组成部分是一个指向std::streambuf
定义如何访问字符序列的指针,例如,文件、字符串、屏幕上的区域等。专门为文件和字符串提供了特殊的流缓冲区和派生自提供流基类以便于创建。描述 IOStreams 库的全部功能几乎可以写一整本书:在 C++ 2003 中,大约一半的库部分专门用于与流相关的功能。
回答by Sandeep_black
Stream is linear queue that connects a file to the program and maintain the flow of data in both direction. Here the source is any file, I/O device, Hard disk, CD/DVD etc.
流是将文件连接到程序并维护双向数据流的线性队列。这里的源是任何文件、I/O 设备、硬盘、CD/DVD 等。
Basically stream is if two type 1.Text Stream 2.Binary stream
基本上流是如果两种类型 1.Text Stream 2.Binary 流
Text Stream : It is a sequence of character arranges in line and each line terminated by new line (unix).
文本流:它是一个按行排列的字符序列,每行以新行(unix)结束。
Binary Stream: It is data as it is coded internally in computer's main memory, without any modification.
二进制流:它是数据,因为它在计算机的主内存中内部编码,没有任何修改。
回答by Farsan Rashid
File system is designed to work with a wide variety of devices, including terminals, disk drives, tape drives etc. Even though each device is different, file system transforms each into a logicaldevice called stream. Streams are device independent so same function can be used to write a disk file and a tape file. In more technical term stream provides a abstraction between programmer and actual device being used.
文件系统被设计为与各种各样的设备一起工作,包括终端、磁盘驱动器、磁带驱动器等。即使每个设备不同,文件系统也会将每个设备转换为称为流的逻辑设备。流是独立于设备的,因此可以使用相同的功能来写入磁盘文件和磁带文件。在更专业的术语中,流提供了程序员和正在使用的实际设备之间的抽象。