File descriptor and file pointer
It is an abstract concept used to express references that only want files. In Linux, each process will save a file descriptor table in the process control block PCB. The file descriptor is the index of this table, and each table has a reference.
Each Linux process should have three standard file descriptors, corresponding to three standard streams. The file descriptor is a non negative integer in form. In fact, it is an index value that points to the record table that the kernel opens the file for each process.
Cheng Cheng. A stream could even be infinitely long. Sometimes, the length of the stream is knowable, but simply disregarded. And sometimes, the length is known but not in usable units. A program reading lines of variable length from a stream probably can't do anything useful with the length of the stream in bytes. We can refer to the data going through the pipe as a stream. The sender will send some bytes and later some more, until it eventually signals that no more will follow.
It will get some bytes and later some more, until it's eventually notified that the end of the stream has been reached. Sometimes, it's more about how the data is treated. For example, reading from a file is often treated as reading from a stream. While it's possible to obtain the length of a file, this information is often disregarded. Instead, the programs that disregard this information just keeps pulling bytes or lines from the file handle until it receives an indication that it reached the end of the stream.
Random access is an example of a file not being treated as a stream. Random access refers to the practice of retrieving data from arbitrary locations of the file. One might do this when one has an index of what's found in the file. An index is some mapping between a key and the location of the item identified by that key in the file.
For example, if I know the data pertaining to a user is found at a certain location in a file, I can request that portion of the file from the OS rather than reading the file from the start. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?
Collectives on Stack Overflow. Learn more. Difference between "file pointer", "stream", "file descriptor" and Ask Question. Asked 1 year, 7 months ago. Active 1 year, 1 month ago. Viewed times. Related: What's the difference between a file descriptor and file pointer? Specification of file descriptors I asked this difference between file descriptor and socket file descriptor.
Improve this question. It's all more or less the same conecpt. FILE pointer, file descriptor, stream, handle, etc. The members of the FILE structure are irrelevant and implementation specific. You don't want to and should not care about them. The FILE struct contains this file descriptor amongst other things such as end-of-file and error indicator, stream position etc. So using fopen gives you a certain amount of abstraction compared to open. In general you should be using fopen since that is more portable and you can use all the other standard C functions that uses the FILE struct, i.
This definition may vary from one compiler to another. File descriptor API is low-level, so it allows to work with sockets, pipes, memory-mapped files and regular files, of course. C1X is offering x modes, so you can fopen with modes "rx" , "wx" , etc.
If you use open , you might consider open See, for example, Do not make assumptions about fopen and file creation. I found a good resource here , giving high level overview of differences between the two:. When you want to do input or output to a file, you have a choice of two basic mechanisms for representing the connection between your program and the file: file descriptors and streams.
File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device such as a terminal , or a pipe or socket for communicating with another process, as well as a normal file.
But, if you want to do control operations that are specific to a particular kind of device, you must use a file descriptor; there are no facilities to use streams in this way. You must also use file descriptors if your program needs to do input or output in special modes, such as nonblocking or polled input see File Status Flags. Streams provide a higher-level interface, layered on top of the primitive file descriptor facilities. The stream interface treats all kinds of files pretty much alike—the sole exception being the three styles of buffering that you can choose see Stream Buffering.
The main advantage of using the stream interface is that the set of functions for performing actual input and output operations as opposed to control operations on streams is much richer and more powerful than the corresponding facilities for file descriptors. The file descriptor interface provides only simple functions for transferring blocks of characters, but the stream interface also provides powerful formatted input and output functions printf and scanf as well as functions for character- and line-oriented input and output.
Since streams are implemented in terms of file descriptors, you can extract the file descriptor from a stream and perform low-level operations directly on the file descriptor. You can also initially open a connection as a file descriptor and then make a stream associated with that file descriptor. In general, you should stick with using streams rather than file descriptors, unless there is some specific operation you want to do that can only be done on a file descriptor.
Understanding individually File Descriptor is an integer value at the kernel level which identifies the file to be operated during the program. Here, its worth mentioning, that everything on Linux is a file.
In C system programming, we have following method to open a file: Code:. Last edited by a moderator: Jan 21, Trinity , Aug 25,
0コメント