Keep in mind that when using fprintf()
I'm aware that I need to pass the file descriptor for writing to the pipe. I just have the doubt and wish to know right now; I don't really have any kind of sample code.
In addition, I want to know if functions such as fputc(), fputs() will also work.
Using fprintf()
requires a file stream ( FILE *
). When you create a pipe, you get two file descriptors. File descriptors are not the same as streams.
You can use the POSIX fdopen()
function to create a file stream from a file descriptor; you can then use fprintf()
on that file stream — or any of the other standard I/O functions that take an explicit file stream argument and write to the stream. You can't use fseek()
; you'll get an error ( ESPIPE
— illegal seek).
You can (in theory) use the POSIX dprintf()
function to write directly to a file descriptor — if your system supports it.
You can use fdopen()
to convert a file descriptor to a FILE *
, and then use all the stdio functions.
int pipefd[2];
pipe(pipefd);
FILE *pipeout = fdopen(pipefd[1], "w");
fprintf(pipeout, "Message written to the pipe\n");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.