c++ - Getting the file-mode from the FILE struct? -
i have piece of c code, function specific, operates on file*
.
depending on mode file*
opened there things can , cannot do.
is there way can obtain mode file*
opened with?
that file*
info can rely on, because created somewhere else in program , actual file-name long lost before reaches function, , cannot influence.
i prefer portable solution.
edit: i'm not interested in file-restrictions specifying users can file. irrelevant dealt upon file-opening. bit of code care open-mode.
on posix (and sufficiently similar) systems, fcntl(fileno(f), f_getfl)
return mode/flags open file in form passed open
(not fopen
). check whether opened read-only, read-write, or write-only, can like:
int mode = fcntl(fileno(f), f_getfl); switch (mode & o_accmode) { case o_rdonly: ... case o_wronly: ... case o_rdwr: ... }
you can check flags o_append
, etc.
Comments
Post a Comment