gpt4 book ai didi

c - 流和文件有什么区别?

转载 作者:太空狗 更新时间:2023-10-29 17:10:34 25 4
gpt4 key购买 nike

C 标准谈​​论流。例如 fopen(3)手册页告诉 fopen 是一个流打开函数

谁能解释一下到底是什么,以及它们与文件的关系?

最佳答案

在 C 标准库的上下文中,流是执行某些 I/O 操作的通用接口(interface)。您可以从流中读取,写入流,有些流是可搜索的。将文件作为流打开只是将流作为应用程序的 I/O 接口(interface)的一种方法。

Let me quote:

11.1.1 Streams and File Descriptors

When you want to do input or output to a file, you have a choice of two basic mechanisms for representing the connection between your program and the file: file descriptors and streams. File descriptors are represented as objects of type int, while streams are represented as FILE * objects.

File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device (such as a terminal), or a pipe or socket for communicating with another process, as well as a normal file. [...]

... and further:

12.1 Streams

For historical reasons, the type of the C data structure that represents a stream is called FILE rather than “stream”. Since most of the library functions deal with objects of type FILE *, sometimes the term file pointer is also used to mean “stream”. This leads to unfortunate confusion over terminology in many books on C.

C 中 I/O 流的例子:

要进一步阅读,还可以查看这些链接:

基于流的 API 构建在低级文件描述符 API 之上,并提供额外的功能。然而,一些低级功能只能通过低级 API 获得,例如,内存映射 I/O非阻塞 I/O 或“事件驱动”输入/输出:

关于c - 流和文件有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com