Re: open an existing file in buffer and write on it



Le 25/01/2013 17:02, Damien Caliste a Ãcrit :
[...]

The idea when you want to change a file on disk is (not too big) :
- generate a buffer of the full content of the file in memory, using
  GString for instance
  (http://developer.gnome.org/glib/unstable/glib-Strings.html) since
  they are automatically expending in size but can be used with
  equivalent printf() convenient functions ;

Not exactly in this case I think.  If the file represents structured
data, as OP's file seem to do, what you wanna do is read the file into a
data structure representing that content, e.g. a list of something like:

struct LibraryEntry {
        gchar *author;
        gchar *editor;
        gchar *title;
        /* whatever */
};

And when writing, serialize that data back to the file's format.  This
makes it easy to manipulate the data in your program, because a buffer
is nothing like easy to deal with.

Regards,
Colomban

- copy atomicaly the buffer in the file with g-file-set-contents()
  http://developer.gnome.org/glib/unstable/glib-File-Utilities.html#g-file-set-contents
- delete the buffer.

Damien.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]