GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Dec 20, 2014 5:18 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: open an existing file in buffer and write on it
PostPosted: Fri Jan 25, 2013 1:55 pm 
Offline
GTK+ Guru

Joined: Sun Jul 08, 2012 3:14 pm
Posts: 107
Location: Coventry, UK
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?
Code:
/* 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");
  }
  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.
Which has probably succeeded as I can see the new entry in treeview, but not in file. Instead, in file, I am only getting Hello World!
Please help


Top
 Profile  
 
 Post subject: Re: open an existing file in buffer and write on it
PostPosted: Thu Feb 14, 2013 9:34 am 
Offline
Familiar Face

Joined: Thu Aug 27, 2009 9:44 pm
Posts: 21
In the last part of your code

Code:
    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);


you try to write into the file by means of printf. But the only purpose of printf is to write to stdout.

You have to replace printf with fprintf and at least the last part of your code should work. For more details see the manual page of printf.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group