什么是文件句柄以及它对程序员有什么用处?
我正在学习汇编语言和 C。我开始讨论“文件句柄”、用于屏幕显示的文件句柄和用于键盘输入的文件句柄等。我不知道什么是文件句柄?我指的是 Peter Abel 的 IBM PC 汇编语言编程
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在学习汇编语言和 C。我开始讨论“文件句柄”、用于屏幕显示的文件句柄和用于键盘输入的文件句柄等。我不知道什么是文件句柄?我指的是 Peter Abel 的 IBM PC 汇编语言编程
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
在计算机软件 API 的上下文中,有一个通用概念通常称为“句柄”。在评论中,您可能找到了有关该主题的维基百科文章的链接。
您正在处理句柄数据类型的特定实现——从
int 0x21
接口返回的 IBM PC/DOS 文件句柄。如果您想了解有关这些特定文件句柄的更多信息,您可能需要查阅这本书 Undocumented DOS,它详细介绍了内存中的数据结构,使您可以进一步研究这些句柄。另一种特定类型的句柄是从名为
open()
的 POSIX 标准接口返回的文件描述符。该函数在 Linux、Windows NT、Mac OS 和许多其他系统等平台上的 C 运行时库中实现。调用open()
返回的整数不能是负数。除非您在 DOS 下运行,否则您的文件句柄可能由 Windows NT 操作系统提供。这些文件句柄从
CreateFile()
(用于打开和创建文件)返回,从此函数返回的句柄的唯一非法值是INVALID_HANDLE_VALUE
。即,Windows NT API 可能会返回被视为(通过强制转换)的“负”整数,尽管它已打开该文件。在所有这些情况下,文件句柄用于引用一些跟踪文件打开方式的数据结构。跟踪的一件重要事情是当前文件位置。位置或指针在 POSIX 中由
lseek()
函数设置,并由tell()
函数读取。任何read()
或write()
都从当前文件指针的位置开始执行。您的程序可以在两个不同的句柄下打开同一文件。在这种情况下,每个句柄的文件指针是不同的。使用 lseek() 更新一个句柄的文件指针不会影响同一文件的另一个句柄的文件指针。
There is a generic concept generally called a "handle" in the context of computer software APIs. In the comments you have probably found a link to the Wikipedia article on that subject.
You are dealing with a specific implementation of a handle data type -- the IBM PC/DOS file handles returned from the
int 0x21
interface. If you would like to learn more about these specific file handles, you might want to consult the book Undocumented DOS, which details the in-memory data structures which allow you to investigate these handles further.Another specific type of handle is the file descriptor returned from the POSIX-standard interface named
open()
. This function is implemented in the C run-time library on platforms such as Linux, Windows NT, Mac OS, and many other systems. The integer returned from a call toopen()
may not be a negative number.Unless you are running under DOS, your file handles are probably provided by the Windows NT Operating System. These file handles are returned from
CreateFile()
(which is used to open as well as create files), and the only illegal value for a handle returned from this function isINVALID_HANDLE_VALUE
. I.e., the Windows NT API may return what would be considered (via casting) a "negative" integer, although it has opened the file.In all of these cases, the file handle is used to refer to some data structure that keeps track of how the file is open. One important thing which is tracked is the current file position. The position or pointer is set in POSIX by the
lseek()
function and is read by thetell()
function. Anyread()
orwrite()
takes place from the position of the current file pointer.Your program can open the same file under two different handles. In this case, the file pointer for each handle is distinct. Updating the file pointer of one handle using
lseek()
will not affect the file pointer of the other handle to the same file.文件句柄是一个整数值,用于寻址打开的文件。此类句柄与操作系统高度相关,但在支持 open() 调用的系统上,您可以像这样创建一个句柄:
然后您可以在读/写调用中使用该句柄。句柄的不可移植性意味着大多数人会避免使用它们,而是使用 C 中的流库函数,例如 fopen、fread、fwrite 等。
A file handle is an integer value which is used to address an open file. Such handles are highly operating system specific, but on systems that support the open() call, you create a handle like this:
You can then use the handle with read/write calls. The non-portability of handles mean that most people avoid them and instead use the stream library functions in C, such as fopen, fread, fwrite etc.
句柄是内核在内部使用来访问某些资源的东西。只有内核真正知道这意味着什么,用户进程只有在想要访问该资源时才被告知要使用什么值。它们还有另一个优点,即文件句柄可以在进程之间共享 - 而您不能使用指针来做到这一点。
Windows 到处都使用句柄...文件、位图、设备上下文、字体等。
A handle is something the kernel uses internally to access some resource. Only the kernel really knows what it means, the user process is only told what value to use when it wants to access this resource. They have another advantage in that file handles can be shared among processes - whereas you can't do this with pointers.
Windows uses handles all over the place... files, bitmaps, device contexts, fonts, etc.