On Fri, 23 Apr 2004, phantom wrote:
[color=blue]
> Hi All.
>
> I am having a problem writing to a file. I can successfully open the
> file and write the contents using fprintf, however if the writing is
> not done for a while in the process the file write returns a status
> -1.[/color]
There could be any number of reasons. The -1 means that you have lost
access to the file. This is why checking the results are important. Some
possible reasons:
1) You ran out of disk space (physically or due to a quota)
2) Something outside your code highjacked the file on you (backup programs
sometimes do this)
3) Something about the OS setup can lead to this happening. I have seen
systems where fopen will mount a file share but it has a timeout. If
nothing writes to it for a time period the file share unmounts and your
file is automatically closed or lost.
Bottom line, it is probably something OS dependant. Try asking in a
newsgroup that deals with your OS and maybe look at what else is running
on the system.
[color=blue]
> To elaborate here is the code segment
>
> main (... , ...)
> {
> if (openLogFile() == 0 )
> {
> exit(1);
> }
>
> printToLog("Starting to write \n");
>
> while (some condition)
> {
> if (condition met) {
>
> printToLog("record being inserted \n");
> }
> }
>
> printToLog ("End of write \n");
> }
>
>
> where openLogFile is as below
> logger.pgc
>
> FILE *fp;
>
> int openLogFile()
> {
> fp = fopen ("filename", "a");
> }
>
> int printToLog (char * mesg)
> {
> int retVal = fprintf(fp, "%s", mesg);
> printf(" return value is %d \n", retVal); // testing purpose only
> }
>
> close()
> {
> fclose(fp);
> }
>
> As above if the printToLog is not called in the while loop for some
> time the printToLog after the while loop returns me a -1. when trying
> to write the final statement. However if the buffer is written in the
> while loop continuously the final write is all good. I have been out
> of C for a while and cant pin what s going on exactly. Also this is
> pgc postgres compiled C and the app is an ecpg app if that helps.[/color]
How about adding an fflush() call to the loop? It will write nothing but
might keep the file open.
[color=blue]
> All help would be greatly appreciated.
>
> Thanks a ton
> Sid.
>[/color]
--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to
vice.president@whitehouse.gov