简体   繁体   English

C ++-缓冲区和流都需要什么?

[英]C++-What is the need of both buffer and stream?

Although i have read about buffer and stream and it's working with files in c++ but i don't know what is the need of a buffer if a stream is there, stream is always there to transfer the data of one file to the program. 虽然我已经阅读了有关缓冲区和流的信息,并且它正在使用c ++中的文件进行工作,但是我不知道如果有流,那么缓冲区的需求是什么,但流总是在那里将一个文件的数据传输到程序中。 So why do we use buffers to store data(performing same task that stream does) and what are buffered and unbuffered stream. 那么为什么我们要使用缓冲区存储数据(执行与流相同的任务)以及缓冲的和未缓冲的流。

Consider a stream that writes to a file. 考虑写入文件的流。 If there were no buffer, if your program wrote a single byte to the stream, you'd have to write a single byte to the file. 如果没有缓冲区,则如果程序将单个字节写入流,则必须将单个字节写入文件。 That's very inefficient. 那是非常低效的。 So streams have buffers to decouple operations one on side of the stream from operations on the other side of the stream. 因此,流具有将流一侧的操作与流另一侧的操作解耦的缓冲区。

A buffer and a stream are different concepts. 缓冲区和流是不同的概念。

A buffer is a part of the memory to temporarily store data. 缓冲区是内存的一部分,用于临时存储数据。 It can be implemented and structured in various ways. 它可以通过多种方式实现和构建。 For instance, if one wants to read a very large file, chunks of the file can be read and stored in the buffer. 例如,如果要读取一个很大的文件,则可以读取文件的块并将其存储在缓冲区中。 Once a certain chunk is processed the data can be discarded and the next chunk can be read. 一旦处理了某个块,就可以丢弃数据,然后可以读取下一个块。 A chunk in this case could be a line of the file. 在这种情况下,块可能是文件的一行。

Streams are the way C++ handles input and output. 流是C ++处理输入和输出的方式。 Their implementation uses buffers. 它们的实现使用缓冲区。

Ok lets lets start from the scratch suppose you want to work with files. 好吧,让我们从头开始假设您要使用文件。 For this purpose you would have to manage how the data is entered into your file and if the sending of data into the file was successful or not, and all other basic working problems. 为此,您将必须管理如何将数据输入文件,是否成功将数据发送到文件以及所有其他基本工作问题。 Now either you can manage all that on your own which would take a lots a time and hard work or What you can do is you can use a stream. 现在,您可以自己管理所有这一切,这将花费大量时间和精力,或者您可以使用流来做。

Yes, you can allocate a stream for such purposes. 是的,您可以为此目的分配流。 Streams work with abstraction mechanism ie we c++ programmers don't know how they are working but we only know that we are at the one side of a stream (on our program's side) we offer our data to the stream and it has the responsibility to transfer data from one end to the other (file's side) 流使用抽象机制工作,即我们c ++程序员不知道它们的工作方式,但是我们只知道我们处于流的一侧(在程序一侧),我们向流提供数据,并且它有责任将数据从一端传输到另一端(文件一侧)

Eg- 例如-

ofstream file("abc.txt"); //Here an object of output file stream is created
file<<"Hello";            //We are just giving our data to stream and it transfers that
file.close();             //The closing of file

Now if you work with files you should know that working with files is a really expensive operation ie it takes more time to access file than to access memory and we also don't have to perform file operations every time. 现在,如果您使用文件,则应该知道使用文件是一项非常昂贵的操作,即访问文件要比访问内存花费更多的时间,我们也不必每次都执行文件操作。 So programmers created a new feature called buffer which is a part of computer's memory and stores data temporarily for handling files. 因此,程序员创建了一个称为缓冲区的新功能,该功能是计算机内存的一部分,并临时存储数据以处理文件。

Suppose at the place of reading file every time to read data you just read some memory location where all the data of file is copied temporarily.Now it would be a less expensive task as you are reading memory not file. 假设在每次读取文件的位置读取数据,您只是读取某个存储位置,在该位置临时复制了文件的所有数据。现在,这将是一项较便宜的任务,因为您正在读取文件而不是文件。

Those streams that have a buffer for their working ie they open the file and by default copy all the data of file to the buffer are called as buffered streams whereas those streams which don't use any buffer are called as unbuffered streams. 具有工作缓冲区的流(即它们打开文件并将默认情况下将文件的所有数据复制到缓冲区)称为缓冲流,而不使用任何缓冲区的流称为未缓冲流。

Now if you enter data to a buffered stream then that data will be queued up until the stream is not flushed (flushing means replacing the data of buffer with that of file). 现在,如果您将数据输入到缓冲流中,则该数据将排队等待,直到不刷新该流为止(刷新意味着将缓冲区的数据替换为文件的数据)。 Unbuffered streams are faster in working (from the point of user at one end of the stream) as data is not temporarily stored into a buffer and is sent to the file as it comes to the stream. 未缓冲的流的工作速度更快(从用户的角度来看,位于流的一端),因为数据不会临时存储到缓冲区中,而是在流中被发送到文件中。

I do agree that stream is probably the poorest written and the most badly udnerstood part of standard library. 我确实同意,stream可能是标准库中最差的文字,也是最难理解的部分。 People use it every day and many of them have not a slightest clue how the constructs they use work. 人们每天都在使用它,其中许多人丝毫不知道他们使用的结构如何工作。 For a little fun, try asking what is std::endl around - you might find that some answers are funny. 为了获得一些乐趣,请尝试询问周围的std::endl是什么-您可能会发现一些答案很有趣。

On any rate, streams and streambufs have different responsibilities. 无论如何,溪流和溪流有不同的责任。 Streams are supposed to provide formatted input and output - that is, translate an integer to a sequence of bytes (or the other way around), and buffers are responsible of conveying the sequence of bytes to the media. 流应该提供格式化的输入和输出-也就是说,将整数转换为字节序列(或相反),而缓冲区负责将字节序列传递给媒体。

Unfortunately, this design is not clear from the implementation. 不幸的是,这种设计从实现上还不清楚。 For instance, we have all those numerous streams - file stream and string stream for example - while the only difference between those are the buffer. 例如,我们拥有所有众多的流-例如文件流和字符串流-而两者之间的唯一区别就是缓冲区。 The stream code remains exactly the same. 流代码保持完全相同。 I believe, many people would redesign streams if they had their way, but I am afraid, this is not going to happen. 我相信,许多人会按照自己的方式重新设计流,但是恐怕这不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM