什么是文件句柄以及它对程序员有什么用处?

发布于 2024-11-09 14:08:11 字数 100 浏览 0 评论 0 原文

我正在学习汇编语言和 C。我开始讨论“文件句柄”、用于屏幕显示的文件句柄和用于键盘输入的文件句柄等。我不知道什么是文件句柄?我指的是 Peter Abel 的 IBM PC 汇编语言编程

I am learning assembly language along with C. this new chapter I started talks about 'file handles', file handles for screen display and file handles for keyboard input etc. I don't know what is a file handle? I am referring to IBM PC ASSEMBLY LANGUAGE PROGRAMMING by Peter Abel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心荒芜 2024-11-16 14:08:11

在计算机软件 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 to open() 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 is INVALID_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 the tell() function. Any read() or write() 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.

Undocumented DOS

撩起发的微风 2024-11-16 14:08:11

文件句柄是一个整数值,用于寻址打开的文件。此类句柄与操作系统高度相关,但在支持 open() 调用的系统上,您可以像这样创建一个句柄:

int handle = open( "foo.txt", OTHER_STUFF_HERE );

然后您可以在读/写调用中使用该句柄。句柄的不可移植性意味着大多数人会避免使用它们,而是使用 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:

int handle = open( "foo.txt", OTHER_STUFF_HERE );

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.

落墨 2024-11-16 14:08:11

句柄是内核在内部使用来访问某些资源的东西。只有内核真正知道这意味着什么,用户进程只有在想要访问该资源时才被告知要使用什么值。它们还有另一个优点,即文件句柄可以在进程之间共享 - 而您不能使用指针来做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文