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



Hi Damien,
Thanks for your reply.
What I have ended up with is something like:
  FILE *fopf = fopen(filename, "a" );
  if (!fopf){
    filename="Untitled.bib";
    fopf= fopen(filename,"a");
  }
  char buffer[]="Hello World";
  int buf_size= strlen(buffer)+1;
  fwrite(buffer,buf_size,1,fopf);
  if(!buffer){
    printf("failed");
  }
//  fclose(fopf);

  GString *fop=g_string_new(NULL);
  g_string_append_printf( fop,buffer);

  gtk_label_set_text(GTK_LABEL(flabel), filename);
    g_string_append_printf( fop, "@%s{%s,\n", strcombo, strkey );
  if( strlen(strAuth)!=0)
    g_string_append_printf( fop, "\tAuthor=\"%s\",\n", strAuth);
  if( strlen(strEditor)!=0)
    g_string_append_printf( fop, "\tEditor=\"%s\",\n", strEditor);
  if( strlen(strSchool)!=0)
    g_string_append_printf( fop, "\tSchool=\"%s\",\n", strSchool);
    g_string_append_printf( fop, "}\n");
//  fclose(fop);
    g_file_set_contents(filename,
        fop,
        1026,
        NULL);
}

But this writes the data in unformatted form. 
Can you kindly explain a bit more?
On Fri, 2013-01-25 at 17:02 +0100, Damien Caliste wrote:
Hello,

Le 25/01/2013, Rudra Banerjee <rudra banerjee aol co uk> a Ãcrit :
But is it so tough? database and all that?
For managing several bibliography files, that should be better yes.

All I want to do is to have the ability of editing an existing file.
Since directly editing the file is not recommended, this is the reason
why I want to open it as buffer!
For just handling one file, that's not necessary of course.

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 ;
- 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.
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list





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