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

29,373

Solution 1

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

It's a handle that identifies the open file; it's generally called a file descriptor, hence the name 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 open returns that integer so that when you pass it read, the operating system can use it to find the resources it needs.

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 open failed 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.

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

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, cout and cerr in C++.

Solution 2

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/etc function

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.

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

Solution 3

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.

Share:
29,373
Rox
Author by

Rox

Updated on September 01, 2020

Comments

  • Rox
    Rox over 3 years

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

    #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 fd represent a file as an integer when passing it to the open method? Is it repesenting a file in current folder? If I print the ´fd´variable, it prints 3. What does that mean?

    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.

    • sth
      sth over 11 years
      Side note: If you are really using C++, std::ifstream would be the much preferable way of reading files.
  • Rox
    Rox over 11 years
    I suppose it is the same thing when using sockets? Like int sock = socket(...);?
  • Mike Seymour
    Mike Seymour over 11 years
    @Rox: Yes; sockets, hardware devices, and pretty much anything else that you read and write data to, are all treated very much like files and stored in the same resource table.
  • Rox
    Rox over 11 years
    The Wikipedia states: The user application passes the abstract key to the kernel through a system call, is it the implementation of openthat makes that system call? Where is the implementation of fcntl.h?
  • Rox
    Rox over 11 years
    The Wikipedia states: The user application passes the abstract key to the kernel through a system call, is it the implementation of open that makes that system call? Where is the implementation of fcntl.h? Is it in glibc?
  • Mike Seymour
    Mike Seymour over 11 years
    @Rox: Yes, I think the open, read, etc. functions are in glibc, and are small wrappers around the corresponding system calls.