“文件指针",“流",“文件描述符"与“文件描述符"之间的区别在于:和...“文件"? [英] Difference between "file pointer", "stream", "file descriptor" and ... "file"?

查看:92
本文介绍了“文件指针",“流",“文件描述符"与“文件描述符"之间的区别在于:和...“文件"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那里有一些相关的概念,即文件指针文件描述符. 我知道文件指针是指向数据类型FILE的指针(例如在FILE.hstruct_FILE.h中声明). 我知道文件描述符int,例如FILE(和_IO_FILE)的成员_fileno.

There are a few related concepts out there, namely file pointer, stream and file descriptor. I know that a file pointer is a pointer to the data type FILE (declared in e.g. FILE.h and struct_FILE.h). I know a file descriptor is an int, e.g. member _fileno of FILE (and _IO_FILE).

关于 流和 file ,我仍在学习.

As for the subtle difference between stream and file, I am still learning.

但来自此处,目前尚不清楚文件状态标志"是否适用于另一种类型的实体. 具体来说,我不知道文件状态标志"是否适用于FILE文件描述符或其他内容. 我正在寻找能显示细节的官方参考.

But from here, I am not clear if there is yet another type of entity to which the "file status flags" apply. Concretely, I wouldn't know if "file status flags" apply to a FILE, to a file descriptor, or what. I am looking for official references that show the specifics.

相关:

>文件描述符和文件之间有什么区别指针?

文件描述符和文件指针之间有什么区别?

>文件指针背后的概念是什么还是流指针?

文件描述符的规范(我问了这个问题)

文件描述符与套接字文件描述符之间的差异

推荐答案

文件句柄

当您首次访问网站时,该网站可能会向您的浏览器提供cookie.该cookie的值将根据浏览器将来的请求自动提供给网站.

When you visit a web site for the first time, the site might provide your browser with a cookie. The value of this cookie will automatically be provided to the web site on future requests by the browser.

此cookie的值可能对您来说很乱,但是对于该特定的Web服务器而言却具有意义.这称为会话ID,它是在某种数据库中查找记录的关键.该记录称为会话.

The value of this cookie is likely gibberish to you, but it has meaning to that one specific web server. It's called a session id, and it's a key to look up a record in some kind of database. This record is called a session.

会话允许Web服务器根据较早的请求和较早请求的结果对一个请求作出响应.例如,它允许服务器知道浏览器在较早的请求中向服务器提供了凭据,并且这些凭据已成功通过身份验证.这就是为什么您每次要以特定用户的身份在StackOverflow上发布/投票/编辑时都不需要重新提供凭据.

Sessions allow the web server to react to one request based on earlier requests and the consequences of earlier requests. For example, it allows the server to know that the browser provided credentials to the server in an earlier request, and that these credentials were successfully authenticated. This is why you don't need to resupply your credentials every time you want to post/vote/edit as a specific user on StackOverflow.

cookie的值(会话ID)是不透明的值.它对您没有任何意义.唯一有用的方法是将其提供回给您的Web服务器.将其提供给另一台Web服务器将无法实现任何有用的功能.这只是识别另一个系统中存在的资源的一种方法.

The cookie's value, the session id, is an opaque value. It doesn't have any meaning to you. The only way it's useful is by providing it back to the web server that gave it to you. Giving it to another web server isn't going to achieve anything useful. It's just a means of identifying a resource that exists in another system.

当另一个系统是操作系统时,我们将这些标识资源的不透明值称为句柄".这绝不是唯一一次以这种方式使用单词handle的方法,但这是最常见的.会话ID cookie为Web服务器提供了一种将Web请求链接在一起的方式,与此类似,一个句柄为OS提供了一种将系统调用链接在一起的方式.有各种资源的句柄.有窗户把手.有用于分配的内存缓冲区的句柄.还有文件句柄.

When that other system is an operating system, we call these resource-identifying opaque values "handles". This is by no means the only time the word handle is used this way, but it's the most common. In much the same way that a session id cookie provides the web server a way of linking web requests together, a handle provides the OS a way of linking system calls together. There are handles for all kinds of resources. There are window handles. There are handles for allocated memory buffers. And there are file handles.

通过在多次调用readwrite的过程中使用相同的文件句柄,操作系统可以知道上一个中断的位置,从而知道从何处继续.它还知道您有权访问正在读取或正在写入的文件,因为这些检查是在打开文件时进行的.

By using the same file handle across multiple calls to read or write, the OS knows where the previous one left off and thus from where to continue. It also knows that you have access to the file from which you are reading or to which you are writing because those checks were done when the file was opened.

文件句柄不仅适用于纯文件.文件句柄还可以引用管道,套接字或许多其他事物之一.创建句柄后,您只需告诉操作系统您要从中读取或写入操作系统,它将使用该句柄来查找执行此操作所需的信息.

File handles aren't just for plain files. A file handle can also reference a pipe, a socket, or one of a number of other things. Once the handle is created, you just have to tell the OS you want to read from it or write to it, and it will use the handle to look up the information it needs to do that.

文件描述符

这是在unix世界中为文件句柄指定的名称.据说open(2)返回文件描述符.据说read(2)带有文件描述符.

This is the name given to file handles in the unix world. open(2) is said to return a file descriptor. read(2) is said to take a file descriptor.

FILE* aka FILE指针aka文件指针

FILE* aka FILE Pointer aka File Pointer

这也是文件句柄.但是与文件描述符不同,它不是来自操作系统. FILE*是C库文件句柄.不能将FILE*传递给read(2)(系统调用),就像将文件描述符传递给fread(3)(C库函数)一样.

This is also a file handle. But unlike a file descriptor, it's not from the OS. A FILE* is a C library file handle. You can't pass a FILE* to read(2) (a system call) any more than you can pass a file descriptor to fread(3) (a C library function).

您永远不要访问FILE的成员,前提是它甚至有任何内容.像所有手柄一样,它对于接收它的用户来说是不透明的.它注定是一个看不见的盒子.违反此约定的代码不能移植,并且可以随时破坏.

You should never access the members of FILE, assuming it even has any. Like all handles, it's meant to be opaque to those receiving it. It's meant to be a box into which you can't see. Code that breaks this convention isn't portable and can break at any time.

大多数C库文件句柄引用包含文件描述符的对象. (由 fmemopen open_memstream 请勿.)它还包括对缓冲的支持,并且可能更多.

Most C library file handles reference an object that includes a file descriptor. (Ones returned by fmemopen and open_memstream don't.) It also includes support for buffering, and possibly more.

文件状态标志

这不是您将永远不需要使用的术语.这是我第一次听到.也许我只是忘记了,因为它并不重要.在链接的文档中,它用于引用一组常量.可以为某些参数提供该组中某些常量的某些组合的各种系统调用.请参阅每个系统的文档,以了解它可以接受哪些标志以及这些标志对它有什么含义.

This is not a term you will ever need to use. It's my first time hearing it. Or maybe I just forgot hearing it because it's not important. In the linked document, it's used to refer to a group of constants. Various system calls can be provided some combinations of some of the constants in this group for certain arguments. Refer to the documentation of each system to see what flags it can accept, and what meanings those flags has to it.

之前,我将文件句柄与会话ID进行了比较.如果会话ID允许Web服务器查找会话,那么用于查找的文件句柄是什么? C库I/O函数的文档将其称为流.

Earlier, I compared file handles to session ids. If a session id allows a web server to look up a session, what is a file handle used to look up? The documentation for the C library I/O functions calls it a stream.

流是一个宽松的术语,通常是指长度不确定的序列.这是通信中常用的术语,是指在编写者/发送者/生产者与阅读者/接收者/消费者之间传递的数据.

A stream is a loose term that usually refers to a sequence of indeterminate length. It's a term commonly used in communication to refer to the data being communicated between a writer/sender/producer and a reader/receiver/consumer.

按顺序访问流,无论它是不必要的还是因为它很方便.跳到信息流中不同点的可能性不会自动使该术语的使用不合格.就像我上面提到的那样,这是一个宽松的名词.

A stream is accessed sequentially, whether it's out of necessity or because it's convenient. The possibility of jumping to a different point in the stream doesn't automatically disqualify the use of the term. Like I mentioned above, it's a loose term.

流的长度通常是未知的.发件人可能甚至不知道.以一个任务为例,该任务可能会从其他流中即时生成一个流.流甚至可以无限长.有时,流的长度是可以知道的,但是却被忽略了.有时,长度是已知的,但不是可用单位.程序从流中读取可变长度的行可能无法对以字节为单位的流长度做任何有用的事情.

The length of a stream is often unknown. It might be even be unknown to the sender. Take for example a task producing a stream on the fly, possibly from other streams. A stream could even be infinitely long. Sometimes, the length of the stream is knowable, but simply disregarded. And sometimes, the length is known but not in usable units. A program reading lines of variable length from a stream probably can't do anything useful with the length of the stream in bytes.

接受两个程序,这些程序通过类似于cat <file1 | cat >file2的管道进行通信.我们可以将流经管道的数据称为流.发送者可能会或可能不会知道最终将发送多少字节/行/消息.发送方将发送一些字节,然后发送更多的字节,直到最终发出信号,表明不再有字节.读者通常不知道生产者最终将发送多少字节/行/消息.它会得到一些字节,然后再得到一些字节,直到最终通知已到达流的末尾.

Take two programs communicating via a pipe like in cat <file1 | cat >file2. We can refer to the data going through the pipe as a stream. The sender may or may not know how many bytes/lines/messages it will eventually send. The sender will send some bytes and later some more, until it eventually signals that no more will follow. The reader often has no idea how many bytes/lines/messages will eventually be sent by the producer. It will get some bytes and later some more, until it's eventually notified that the end of the stream has been reached.

有时候,更多的是关于如何处理数据的信息.例如,从文件读取通常被视为从流读取.尽管可以获取文件的长度,但通常忽略此信息.取而代之的是,忽略此信息的程序只会不断从文件句柄中提取字节或行,直到收到表明已到达流末尾的指示为止.

Sometimes, it's more about how the data is treated. For example, reading from a file is often treated as reading from a stream. While it's possible to obtain the length of a file, this information is often disregarded. Instead, the programs that disregard this information just keeps pulling bytes or lines from the file handle until it receives an indication that it reached the end of the stream.

随机访问是不被视为流的文件的示例.随机访问是指从文件的任意位置检索数据的做法.当人们拥有在文件中找到的内容的索引时,可能会执行此操作.索引是键和文件中由该键标识的项的位置之间的某种映射.例如,如果我知道与用户有关的数据是在文件中的某个位置找到的,那么我可以从OS请求文件的那部分,而不用从头开始读取文件.

Random access is an example of a file not being treated as a stream. Random access refers to the practice of retrieving data from arbitrary locations of the file. One might do this when one has an index of what's found in the file. An index is some mapping between a key and the location of the item identified by that key in the file. For example, if I know the data pertaining to a user is found at a certain location in a file, I can request that portion of the file from the OS rather than reading the file from the start.

这篇关于“文件指针",“流",“文件描述符"与“文件描述符"之间的区别在于:和...“文件"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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