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



ZZ,
thanks for your comment!
But is it so tough? database and all that?
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!

On Fri, 2013-01-25 at 16:10 +0100, zz excite it wrote:
On Friday 25 January 2013 14:57:23 Rudra Banerjee wrote:
Dear friends,
as evident from my last few posts, I am struggling with opening a file
as buffer and write to it
(guess it has *nothing* to do with gtk, but C. Still I will be grateful
if you people kindly help). 
So, first, how to open file in buffer?

/* Files opened and edited directly*/
/*  FILE *fop = fopen(filename, "a" );
  if (!fop){
    filename="Untitled.bib";
    fop= fopen(filename,"a");
  }
*/  

/*Trying to open file as buffer and edit that*/
  FILE *fopf = fopen(filename, "a" );
  if (!fopf){
    filename="Untitled.bib";
    fopf= fopen(filename,"a");
  }


You have to open, read  (and then close) the file into a static or malloced buffer of sufficient size,
then you can operate on the buffer. When your are done you write out the buffer
to a newfile, delete the old and rename the new to old.

  char fop[]="Hello World";
  int buf_size= strlen(fop)+1;
  fwrite(fop,buf_size,1,fopf);
  if(!fop){
    printf("failed");
  }
  fclose(fopf);

  gtk_label_set_text(GTK_LABEL(flabel), filename);
    printf( fop, "@%s{%s,\n", strcombo, strkey );
  if( strlen(strAuth)!=0)
    printf( fop, "\tAuthor=\"%s\",\n", strAuth);
  if( strlen(strEditor)!=0)
    printf( fop, "\tEditor=\"%s\",\n", strEditor);
  if( strlen(strTitle)!=0)
    printf( fop, "\tTitle=\"%s\",\n", strTitle);
//  fclose(fop);


The very first part of the code (commented) used to access the file
directly, an approach that I was advised against. So I tried to open the
file in buffer. But this implementation makes the program crash as soon
as enter some value. 

Please help


PS.: at the first glance it seems to me you need a database backend
like sqlite to save, store, change and retrieve your data.

Ciao,
ZZ
_______________________________________________
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]