Pipe vs file descriptor
On returning from the spawn call, close the duplicate descriptor in the parent process. For more information, see example 2 later in this article. In the Windows operating system, a pipe is destroyed when all of its descriptors have been closed. If all read descriptors on the pipe have been closed, then writing to the pipe causes an error. By default, this function's global state is scoped to the application. To change this, see Global state in the CRT.
For more compatibility information, see Compatibility. All versions of the C run-time libraries. This is a basic filter application. You pass "naked" file descriptors to actual Unix calls, such as read , write and so on. 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. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related Writing code in comment? Please use ide. Load Comments. What's New. Most popular in C Language. More related articles in C Language. We use cookies to ensure you have the best browsing experience on our website.
0コメント