If I create pipe in unix this way:
int fds[] = {0, 0};
pipe(fds);
Then make FILE * from fds[0] this way:
FILE *pipe_read = fdopen(fds[0], "rt");
Then how should I close this file (pipe_read)?
fclose(pipe_read)pclose(pipe_read)close(fileno(pipe_read))
fdopenreturns aFILE*so you shouldfcloseit. This will also close the underlying file descriptor as well.The
pclosecall is meant for closing a handle created withpopen, a function you use for running a command and connecting to it with pipes.The
closecall will close the underlying file descriptor but, unfortunately, before the file handle has had a chance to flush its data out – in other words, you’re likely to lose data.