I need to open a file for writing. If the file already exists, I don't want to truncate it.
In other words, in plain C I'd do:
int fd = open("output.bin", O_WRONLY | O_CREAT, 0666);
// I don't mind using O_RDWR, btw.
I'm trying to do something similar with GLib's GFile (part of GIO). I first tried:
g_file_create(gfile, G_FILE_CREATE_NONE, NULL, NULL);
But this fails if the file already exists.
I see that there are about 5 other functions that return GFileOutputStream
or GFileIOStream
, but I don't quite see one that does what I want.
Am I missing something?
Do I need to split this simple task into several small ones? (checking for file existence; if exists, create, otherwise open; all wrapped somehow in a lock.)
(BTW, if it matters: my file will reside on the local filesystem, not a networked one. Also, I'm working in Vala, which is why I don't simply use open()
(maybe I could find bindings for it, but I prefer to learn the GIO way of doing things).)