C++ 键入时 fd 代表什么:int fd = open("file");?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12214878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:02:08  来源:igfitidea点击:

What does fd represent when typing: int fd = open("file");?

c++fileio

提问by Rox

I am looking at I/O operations in C++ and I have a question. When opening a file like:

我正在研究 C++ 中的 I/O 操作,我有一个问题。打开文件时,如:

#include <fcntl.h>
int main() {
    unsigned char buffer[16];     
    int fd = open (argv[1], O_RDONLY);
    read(fd, buffer, sizeof(buffer));
    return 0;
}

How can the variable fdrepresent a file as an integer when passing it to the openmethod? Is it repesenting a file in current folder? If I print the ′fd′variable, it prints 3. What does that mean?

fd将文件传递给open方法时,变量如何将文件表示为整数?是否重复当前文件夹中的文件?如果我打印 'fd' 变量,它会打印 3。这是什么意思?

Ps. I know there are several other ways to handle files, like stdio.h, fstream etc but that is out of the scope of this question. Ds.

附言。我知道还有其他几种处理文件的方法,例如 stdio.h、fstream 等,但这超出了本问题的范围。D.

回答by Mike Seymour

How can the variable fd represent a file as an integer when passing it to the open method?

将文件传递给 open 方法时,变量 fd 如何将文件表示为整数?

It's a handle that identifies the open file; it's generally called a file descriptor, hence the name fd.

它是一个标识打开文件的句柄;它通常被称为文件描述符,因此得名fd.

When you open the file, the operating system creates some resources that are needed to access it. These are stored in some kind of data structure (perhaps a simple array) that uses an integer as a key; the call to openreturns that integer so that when you pass it read, the operating system can use it to find the resources it needs.

当您打开文件时,操作系统会创建一些访问它所需的资源。它们存储在某种使用整数作为键的数据结构(可能是一个简单的数组)中;调用open返回该整数,以便当您传递它时read,操作系统可以使用它来查找所需的资源。

Is it repesenting a file in current folder?

是否重复当前文件夹中的文件?

It's representing the file that you opened; its filename was argv[1], the first of the arguments that was passed to the program when it was launched. If that file doesn't exist, or openfailed for some reason, then it has the value -1 and doesn't represent any file; you really should check for that before you try to do anything with it.

它代表您打开的文件;它的文件名是argv[1],它是程序启动时传递给程序的第一个参数。如果该文件不存在,或者open由于某种原因失败,那么它的值为 -1 并且不代表任何文件;在尝试对它做任何事情之前,你真的应该检查一下。

If I print the fdvariable, it prints 3. What does that mean?

如果我打印fd变量,它会打印 3。这是什么意思?

It doesn't have any particular meaning; but it has that value because it was the fourth file (or file-like thing) that was opened, after the input (0), output (1) and error (2) streams that are used by cin, coutand cerrin C++.

它没有任何特别的意义;但它具有该值,因为它是打开的第四个文件(或类似文件的东西),在输入 (0)、输出 (1) 和错误 (2) 流之后,由cin,coutcerrC++ 使用。

回答by dynamic

Because that is the index of the table of resources stored for your current process.

因为这是为当前进程存储的资源表的索引。

Each process has it own resources table, so you just need to pass the index to read/write/etcfunction

每个进程都有自己的资源表,所以你只需要将索引传递给read/write/etc函数

Generally, a file descriptor is an index for an entry in a kernel-resident data structure containing the details of all open files. In POSIX this data structure is called a file descriptor table, and each process has its own file descriptor table. The user application passes the abstract key to the kernel through a system call, and the kernel will access the file on behalf of the application, based on the key. The application itself cannot read or write the file descriptor table directly.

通常,文件描述符是内核驻留数据结构中条目的索引,其中包含所有打开文件的详细信息。在 POSIX 中,这种数据结构称为文件描述符表,每个进程都有自己的文件描述符表。用户应用程序通过系统调用将抽象密钥传递给内核,内核将根据密钥代表应用程序访问文件。应用程序本身不能直接读取或写入文件描述符表。

from: http://en.wikipedia.org/wiki/File_descriptor

来自:http: //en.wikipedia.org/wiki/File_descriptor

回答by Aduait Pokhriyal

open() returns the file descriptor of the file which is the C type int. To know more about File Descriptor refer http://en.wikipedia.org/wiki/File_descriptor.

open() 返回文件的文件描述符,它是 C 类型 int。要了解有关文件描述符的更多信息,请参阅http://en.wikipedia.org/wiki/File_descriptor

回答by kmdv

"fd" stands for file descriptor. It is a value identifying a file. It is often an index (in the global table), an offset, or a pointer. Different APIs use different types. WinAPI, for example, uses different types of handles (HANDLE, HGDI, etc.), which are essentially typedefs for int/void*/long, and so on.

“fd”代表文件描述符。它是一个标识文件的值。它通常是一个索引(在全局表中)、一个偏移量或一个指针。不同的 API 使用不同的类型。例如,WinAPI 使用不同类型的句柄(HANDLE、HGDI 等),它们本质上是 int/void*/long 等的 typedef。

Using naked types like "int" is usually not a good idea, but if the implementation tells you to do so (like POSIX in this case), you should keep it.

使用像“int”这样的裸类型通常不是一个好主意,但如果实现告诉您这样做(如本例中的 POSIX),您应该保留它。

回答by mah

The simplified answer is that fdis just an index into some array of file descriptors.

简化的答案是这fd只是某个文件描述符数组的索引。

When most processes are started, they are given three open file descriptors to begin with: stdin(0), stdout(1), and stderr(2). So when you open your first file, the next available array entry is 3.

当大多数进程启动时,它们会被赋予三个打开的​​文件描述符:stdin(0)、stdout(1) 和stderr(2)。因此,当您打开第一个文件时,下一个可用的数组条目是 3。