开发者

Using MinGW how can I create a file without the read-only file attribute?

When I attempt to overwrite an existing file, I get a "permission denied" error. I noticed that the file which is created has the "Read-only" attribute set. When I manually unset this I can then overwrite the file. Is there some flag I can pass to open() which will automatically unset this when I create a file?

Below is a bare bones example which开发者_运维百科 illustrates the problem. The first run works, but the second produces the "permission denied" error.

Thanks, Zach (New to MingW/Windows 7)

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char ** argv) {
    int fid;
    double data = 12.0;

    if ( (fid = open("junk.data", O_WRONLY | O_CREAT | O_BINARY)) == -1 ) {
        printf("ERROR opening.\n\terror is:%s\n", strerror(errno));
        return 1;
    }

    write(fid, &data, sizeof(double));

    close(fid);

    return 0;
}


I tried both 0644 and S_IRUSR | S_IWUSR (with sys/stat.h included) and either works.

Make sure that you actually add it as third argument of open, instead as new term into the surrounding parentheses (as happened for me first, and compiles just fine)


open has a three-parameter variant:

int open(const char *pathname, int flags, mode_t mode);

That third parameter allows you to specify the mode bits on Unix-type systems, but should be enough to set minimal r/w permissions on windows. (Check out the man page for details.)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜