Connecting Tech Pros Worldwide Forums | Help | Site Map

Weird file writing problem

phantom
Guest
 
Posts: n/a
#1: Nov 14 '05
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.
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.

All help would be greatly appreciated.

Thanks a ton
Sid.

Fred L. Kleinschmidt
Guest
 
Posts: n/a
#2: Nov 14 '05

re: Weird file writing problem




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.
> 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.
>
> All help would be greatly appreciated.
>
> Thanks a ton
> Sid.[/color]

Check the value of errno when fprintf returns a negative value.

Also, I am assuming that the above code snippets are quite incomplete -
some of the functions are declared as returning an int, yet there is no
return statement. Why did you include the close() function in this post,
when it is never invoked?

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225
Darrell Grainger
Guest
 
Posts: n/a
#3: Nov 14 '05

re: Weird file writing problem


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
phantom
Guest
 
Posts: n/a
#4: Nov 14 '05

re: Weird file writing problem


"> Check the value of errno when fprintf returns a negative value.[color=blue]
>
> Also, I am assuming that the above code snippets are quite incomplete -
> some of the functions are declared as returning an int, yet there is no
> return statement. Why did you include the close() function in this post,
> when it is never invoked?[/color]


Thank you. You are correct the code snippets are rather incomplete.
how do i get the errorno from the fn (as naive as this may sound)

int printToLog()
{
int oo = fprintf(fp,"%s",mesg);
return oo;
}

I am guessing its not the oo variabe you talking about.

In my effort to track the bug I ran a gdb on the code and had the foll
error.

Program received signal SIGSEGV, Segmentation fault.
0x401f1087 in pthread_mutex_lock () from /lib/i686/libpthread.so.0

My pointers are going stray that i know its strange as the program
works fine for certain inputs and not otherwise indicating something
wrong just that its not too obvious.

Thank you again
Sid
Sean Kenwrick
Guest
 
Posts: n/a
#5: Nov 14 '05

re: Weird file writing problem



"phantom" <shb123in@yahoo.co.in> wrote in message
news:c77d31c3.0404230706.165a35fb@posting.google.c om...[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.
> 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.
>
> All help would be greatly appreciated.
>
> Thanks a ton
> Sid.[/color]

You have not posted enough code to establishg the cause of the problem.
What is it that sets the 'some condition' and 'condition met' conditions in
the if and while statements? If, as I suspect, these are actually
function calls in you program then it is likely that it is down in these
functions that you are overwriting memeory or using an unititialised pointer
or some other common error that is causing undefined behavior. You
should post some more complete code in order to get a sensible answer - and
try and make it conform to Standard 'C' otherwise you will be mocked,
derided and generally hassled on this group for posting poor quality code
;-)

Sean


Closed Thread