Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

file

io

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.

like image 836
Rox Avatar asked Aug 31 '12 11:08

Rox


3 Answers

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++.

like image 182
Mike Seymour Avatar answered Nov 03 '22 12:11

Mike Seymour


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

like image 45
dynamic Avatar answered Nov 03 '22 11:11

dynamic


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.

like image 38
Aduait Pokhriyal Avatar answered Nov 03 '22 10:11

Aduait Pokhriyal