开发者

How to determine if a file can be written using C++

In C++ how can I determine if the program has either read-only access or read-wr开发者_JS百科ite access to a file? I searched the boost filesystem library but I have yet to find something to help me. Right now I thinking of opening the file, trying to write inside and check for error, but that doesn't seem a very appropriate way of doing this.

Any clue?

EDIT : it would need to be cross platform


At the end of the day, the only way to test if you can write data to a file on a modern OS is to actually try to write it. Lots of things could have happened to the file between tests for permission and the actual write.


The system call, which most runtime libraries fully support, is

#include <unistd.h>

if (0 == access (char *pathname, int mode))
    // permission is granted

where mode is F_OK to test existence of file, or a mask consisting of the bitwise OR of one or more of R_OK, W_OK, and X_OK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜