且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

格式化和未格式化的输入和输出和流

更新时间:2022-12-15 16:15:28

格式化IO 意味着您的输出由格式字符串确定,这意味着您提供了一个字符串

Formatted IO means that your output is determined by a "format string", that means you provide a string with certain placeholders, and you additionally give arguments that should be used to fill these placeholders:

const char *daughter_name = "Lisa";
int daughter_age = 5;
printf("My daughter %s is %d years old\n", daughter_name, daughter_age);

示例中的占位符是%s ,表示这应该使用字符串替换,%d ,表示这将被有符号整数替换。有更多的选项,让你控制最终字符串将如何呈现自己。这对于作为程序员来说很方便,因为它减轻了将不同数据类型转换为字符串的负担,并且还通过 strcat 或者任何类似的。

The placeholders in the example are %s, indicating that this shall be substituted using a string, and %d, indicating that this is to be replaced by a signed integer number. There are a lot more options that give you control over how the final string will present itself. It's a convenience for you as the programmer, because it relieves you from the burden of converting the different data types into a string and it additionally relieves you from string appending operations via strcat or anything similar.

另一方面,无格式化的IO意味着你只需将字符或字节序列写入流,而不是使用任何格式字符串。

Unformatted IO on the other hand means you simply write character or byte sequences to a stream, not using any format string while you are doing so.

这使我们了解您对流的问题。 流的一般概念是,你不必加载文件或任何输入作为一个整体的所有时间。对于小数据这虽然工作,但想象你需要处理兆字节的数据 - 没有办法这将适合一个单字节数组,没有你的机器运行内存不足。这就是为什么流式处理允许您以更小尺寸的块处理数据,一次一个,一个接一个,所以在任何给定的时间,你只需要处理大小固定的数据量。您一次又一次地将数据读入辅助变量并处理,直到基础流告诉您已完成,且没有更多数据。

Which brings us to your question about streams. The general concept behind "streaming" is that you don't have to load a file or whatever input as a whole all the time. For small data this does work though, but imagine you need to process terabytes of data - no way this will fit into a single byte array without your machine running out of memory. That's why streaming allows you to process data in smaller-sized chunks, one at a time, one after the other, so that at any given time you just have to deal with a fix-sized amount of data. You read the data into a helper variable over and over again and process it, until your underlying stream tells you that you are done and there is no more data left.

同样的工作在输出端,你写你的输出步骤的步骤,chunk的块,而不是一次写整个东西。

The same works on the output side, you write your output step for step, chunk for chunk, rather than writing the whole thing at once.

这个概念带来了其他好的功能,太。因为您可以在流内嵌入流内的流,您可以构建一个完整的转换链,其中每个流可以修改数据,直到您最终收到最终结果,不知道单个转换,因为您将流视为有只有一个。

This concept brings other nice features, too. Because you can nest streams within streams within streams, you can build a whole chain of transformations, where each stream may modify the data until you finally receive the end result, not knowing about the single transformations, because you treat your stream as if there were just one.

这可能非常有用,例如C或C ++流缓冲他们从本地读取的数据从例如一个文件,以避免不必要的调用和读取优化块中的数据,使得整体性能将比从文件系统直接读取更好。

This can be very useful, for example C or C++ streams buffer the data that they read natively from e.g. a file to avoid unnecessary calls and to read the data in optimized chunks, so that the overall performance will be much better than if you would read directly from the file system.