流和文件有什么区别? [英] What is the difference between a stream and a file?

查看:231
本文介绍了流和文件有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C标准谈论流.例如, fopen(3) 手册页告诉fopen流开放功能.

The C standard talks about streams. For example the fopen(3) manual page tells that fopen is a stream open function.

任何人都可以解释的确切含义以及它们与文件的关系吗?

Can anybody explain what exactly streams are, and how they relate to files?

推荐答案

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

In the context of the C Standard Library a stream is a generic interface for performing certain I/O operations. You can read from streams, write to streams, some streams are seekable. Opening a file as a stream is only one way to get a stream as an I/O interface for an application.

让我引用:

11.1.1流和文件描述符

要对文件进行输入或输出时,可以选择两种基本机制来表示程序与文件之间的连接:文件描述符和流.文件描述符表示为int类型的对象,而流表示为FILE *对象.

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. [...]

...并进一步:

12.1流

出于历史原因,表示流的C数据结构的类型称为FILE,而不是"stream".由于大多数库函数都处理类型为FILE *的对象,因此有时有时文件指针也用于表示流".不幸的是,这导致许多有关C的书籍对术语的困惑.

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流的示例:

Examples for I/O streams in C:

  • Standard Streams: https://linux.die.net/man/3/stdin
  • File Streams: https://linux.die.net/man/3/fopen
  • Pipes: https://linux.die.net/man/3/popen
  • Stream Sockets: https://linux.die.net/man/2/socket

要进一步阅读,请查看以下链接:

For further reading, also have a look at these links:

  • https://www.gnu.org/software/libc/manual/html_mono/libc.html#I_002fO-Overview
  • https://www.gnu.org/software/libc/manual/html_mono/libc.html#I_002fO-on-Streams

基于流的API构建在低级文件描述符 API之上,并提供其他功能.但是,某些低级功能只能通过低级API使用,例如,内存映射I/O 非阻塞I/O 或事件驱动" ; I/O:

The stream-based API is built on top of the low-level file descriptor API and provides additional functionality. Some low-level features are however only available via the lower level API, e.g., memory-mapped I/O, non-blocking I/O or "event-driven" I/O:

  • https://www.gnu.org/software/libc/manual/html_node/Memory_002dmapped-I_002fO.html
  • https://linux.die.net/man/2/poll
  • https://linux.die.net/man/4/epoll

这篇关于流和文件有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆