473,388 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 1495


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,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.