James,
thanks for your elaboration.
But, I have used fprintf instead of g_fprintf, which is, by defination," An implementation of the standard fprintf() function which supports positional parameters, as specified in the Single Unix Specification."
As such, questions about g_fprintf() are best answered in a
UNIX-oriented newsgroup, such as comp.programmer.UNIX.
Also, from
http://www.gnu.org/software/libc/manual/html_node/Buffering-Concepts.html, I think fprintf is "unbuffered", as it reads/writes directlyto the file.
Why would you think that? it says "Newly opened streams are normally
fully buffered, with one exception: a stream connected to an interactive
device such as a terminal is initially line buffered." A file is
unbuffered only if you explicitly call setvbuf() to make it unbuffered.
What I am trying to achieve is read the whole file in buffer and edit the buffer only! The buffer will flush to file when that is filled....something in that way(guess I am too vague here)!
The way to do that in C is to fread() into a buffer, edit the buffer,
and then fwrite() it.
The buffer you're editing will be different from the buffer associated
with the file, but copying between those buffers is not going to cost
you much time by comparison with the file I/O. You could avoid those
copies by making both the files unbuffered, but I would recommend that
approach only if you're going to have but a single fread() and a single
fwrite(). If there are multiple reads and writes you're better letting
the C standard library handle the I/O buffer, while you concentrate on
your internal data buffer.
I was trying something like:
FILE *fopf = fopen(filename, "a" );
if (!fopf){
filename="Untitled.bib";
fopf= fopen(filename,"a");
Note: you make no attempt to check whether the second fopen() failed.
It's quite possible for it to fail, and the behavior of your program is
undefined if it does.
}
char fop[]="Hello World";
int buf_size= strlen(fop)+1;
fwrite(fop,buf_size,1,fopf);
fclose(fopf);
You also failed to check whether fwrite() or fclose() failed. Because
fopf IS buffered, a failed fclose() implies that there might still be
data in the buffer which has not yet been written to the file.
if( strlen(strAuth)!=0)
g_fprintf( fop, "\tAuthor=\"%s\",\n", strAuth);
but that caused the app to crash, with Segmentation fault (core dumped)while writing to it.
I don't have documentation for g_fprintf(), but in your first message,
you passed it a FILE* as the first argument, and in this message, you're
passing a char*. One of the two is probably incorrect. Since this is the
one that failed, it's probably the incorrect one.
I think you meant to use fopf rather than fop - but if so, that's still
problematic. Your program should not have closed fopf until after you
were finished working with it.