472,784 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

Weird file writing problem

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.
Nov 14 '05 #1
4 1460


phantom wrote:

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.


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
Nov 14 '05 #2
On Fri, 23 Apr 2004, phantom wrote:
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.
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.
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.
How about adding an fflush() call to the loop? It will write nothing but
might keep the file open.
All help would be greatly appreciated.

Thanks a ton
Sid.


--
Send e-mail to: darrell at cs dot toronto dot edu
Don't send e-mail to vi************@whitehouse.gov
Nov 14 '05 #3
"> 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?

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
Nov 14 '05 #4

"phantom" <sh******@yahoo.co.in> wrote in message
news:c7**************************@posting.google.c om...
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.


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
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian | last post by:
Here's the example: You have a world writable file called lastquote.cfg on your server with a one or two digit number in it. When you run the script below, it reads the value, increments the...
11
by: ncf | last post by:
Ok, I've been tring to resolve this issue for some time now (~1 day which is way longer than normal for me) to no avail. I am reading a file into a list in memory, using a "%" delimited file...
6
by: queisser | last post by:
Hi all, I'm writing a C# app that uses copy/paste to put graphics in a window. My copy/paste logic first looks for Metafile, then for bitmap. Here's the problem: when I copy and paste a WMF...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
2
by: Jolly Green Giant | last post by:
I'm trying to use PHP to create a text file. Simple right? I've done it before. I don't know why it's not working now. It's not rocket science. In general, I'd say I'm a PHP beginner, but with...
2
by: sieg1974 | last post by:
Hi, I have a linked list with 705 nodes, and the functions getContact and listContact to deal with it. listContact works properly, and prints all some debug information about each node. On the...
0
by: P Pulkkinen | last post by:
Dear all, sorry, i know this code is far little too long to debug here, but there is really annoying logical error. If someone debugs this, I really offer warm virtual handshake. What this...
20
by: ongaro.admin | last post by:
Hi, I'm experiencing a strange problem with .mdb files. We have two buildings connected by optical fiber (a single LAN). Everything works perfect with any file, any size, any application...
4
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems,...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.