CreateFile CREATE_NEW equivalent on linux

后端 未结 3 1711
清歌不尽
清歌不尽 2021-01-14 05:40

I wrote a method which tries to create a file. However I set the flag CREATE_NEW so it can only create it when it doesnt exist. It looks like this:

for (;;)
         


        
3条回答
  •  灰色年华
    2021-01-14 06:19

    Take a look at the open() manpage, the combination of O_CREAT and O_EXCL is what you are looking for.

    Example:

    mode_t perms = S_IRWXU; // Pick appropriate permissions for the new file.
    int fd = open("file", O_CREAT|O_EXCL, perms);
    if (fd >= 0) {
        // File successfully created.
    } else {
        // Error occurred. Examine errno to find the reason.
    }
    

提交回复
热议问题