cfileglibread-writegio

How can I open a file such that I can both read from it and write into it with Gio/GLib in C?


I'm writing a program where I want to rewrite an existing configuration file. I am writing the parser for this format myself, so I can't re-use an existing parser. I want to use GLib/Gio's utility functions for this, but I can't seem to figure out how to open a file as readwrite with them. I found Gio.IOStream and Gio.FileIOStream, but I haven't been able to figure out how to actually go about opening a file so that I get one of these. With Gio.FileInputStream, I was able to acquire one with g_file_read (), but I haven't been able to find any such equivalent for Gio.FileIOStream.

How would I go about opening a file such that I can read it until I find some matching string and then write to it at that position with Gio/GLib in C?


Solution

  • To open a file for both reading and writing with Gio, you can use g_file_open_readwrite (). This will give you a GFileIOStream, which you then can can split into a GInputStream with g_io_stream_get_input_stream () and a GOutputStream with g_io_stream_get_output_stream for reading from and writing to it, respectively.