cfile-ioprintffdopen

How to fdopen as open with the same mode and flags?


I would like to have a FILE* type to use fprintf. I need to use fdopen to get a FILE* instead of open that returns an int. But can we do the same with fdopen and open? (I never used fdopen)

I would like to do a fdopen that does the same as :

open("my_file", 0_CREAT | O_RDWR | O_TRUNC, 0644);

Solution

  • fdopen takes a file descriptor that could be previously returned by open, so that is not a problem.

    Just open your file getting the descriptor, and then fdopen that descriptor.

    fdopen simply creates a userspace-buffered stream, taking any kind of descriptor that supports the read and write operations.