hi....,
can anyone tell me what is the exact difference between file descriptor and file pointer...... and why file descriptor takes integer value???
File descriptor is an integer which is an index in the kernel on the opened files(Which is called file descriptor table).It is used to deal with the files . most of the functions like open,close,read using the file descriptors to deal with the files.
File pointer is a location with in the file.Which points the next character which going to read.
While I would mostly agree with the file descriptor definition, a file pointer is not usually what you describe but commonly used to refer to what the standard C library uses to handle files (FILE *).
The associated functions are fopen, fclose, fread, fwrite, fscanf and the likes.
I would also note that a file pointer has an associated file descriptor used by the library functions to do the actual I/O calls to the kernel. You can get the associated fd using the fileno function.
So shall I understand that the filedescriptor is number of amount connections for particular process/program which will be used ? and its predefined ?
A file descriptor is an index in a per process table. It isn't predefined outside 0, 1 and 2 which are stdin, stdout and stderr when the process starts.
It's not a count of anything, it's just an arbitrary number assigned to that process when it opens a file. The kernel will recognize that number when you make a read() or write() call with it and respond accordingly.
By tradition, certain file numbers are expected to be open by default. stdin should be connected to some sort of input like keyboard or file, stdout should be connected to some sort of output like a screen or file, and stderr should either be connected to a terminal or to nothing. But this is just by tradition. The kernel really doesn't care.
A file pointer is what you get when you open a file with the stdio library. It's a structure that holds buffers and so forth, things that raw file descriptors don't have, but at the heart stdio still uses fd's, at least on UNIX sysems:
FILE *fp=fopen("filename", "r");
if(fp != NULL)
{
printf("fp's descriptor is %d\n", fileno(fp));
}
Not completely arbitrary. The lower unused index available is always used when a file is successfully open.
I wouldn't call it a tradition as it is a quite established POSIX standard (STDIN_FILENO and so on).
It wouldn't matter if the kernel assigned them randomly, for most purposes. (assuming dup2 still works, of course.) In that respect it's arbitrary.
Yes, stdin/stdout/stderr are a standard, but not one enforced by the kernel. Only to programs are FDs 0,1,2 anything special.
You are right it wouldn't matter that much but the fact is every Unix/Unix like implementation is assigning file descriptor sequentially and is doing so because it is both the most logical and the standard compliant/documented way to do it.
I don't get your std* remark. File descriptors are only relevant when referring to processes. Unix/Unix like kernels are handling files objects with file/vnode/whatever structures but not file descriptors.
I'm just saying there's nothing in particular enforcing stdin/stdout/stderr. They don't get special treatment from open/close/read/write, they're just prepared in advance.