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 (;;)
fd = open("path/to/file", O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC);
O_CREAT: Creates file if it does not exist. If the file exists, this flag has no effect.
O_EXCL: If O_CREAT and O_EXCL are set, open() will fail if the file exists.
O_RDWR: Open for reading and writing.
Also, creat() is equivalent to open() with flags equal to O_CREAT|O_WRONLY|O_TRUNC.
Check this: http://linux.die.net/man/2/open