473,395 Members | 2,436 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,395 software developers and data experts.

Problem with fopen

I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following:

FILE *fd;
fd = fopen("file.log","w");

fprintf(fd, "%s\n",<a character string>);

This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.

Can anyone help?

Thanks,
Vaddadi

Jan 29 '06 #1
3 2373
In article <11**********************@f14g2000cwb.googlegroups .com>,
va************@gmail.com <va************@gmail.com> wrote:
I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following: FILE *fd;
fd = fopen("file.log","w"); fprintf(fd, "%s\n",<a character string>); This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it. Can anyone help?


Threads are not part of standard C, so a newsgroup such as
comp.programming.threads or comp.programming.unix would be suggested.
<OT>
My -guess-, given what little you've said, is that you are attempting
to write to the file in a different thread than you opened it in,
and that in that other thread you have not taken the proper POSIX
precautions to be allowed to write. In POSIX, when you want to
write to the same descriptor from different processes or different
threads, you first have to synchronize the I/O between the threads.
The proper procedures are spelt out in the POSIX documentation, and
the details are not appropriate for comp.lang.c .
--
Prototypes are supertypes of their clones. -- maplesoft
Jan 29 '06 #2
va************@gmail.com wrote:
I am writing a multithreaded web-server and multithreaded client in C.
The server is required to keep a log of the transactions in a local
file. I try to open a file using the following:

FILE *fd;
fd = fopen("file.log","w");

fprintf(fd, "%s\n",<a character string>);

This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.

Can anyone help?


The library and/or the operating system is probably
buffering the output you write to the file, accumulating
the data in memory until there's enough to make an actual
write worth while. If you wait until "enough" output has
been generated, it's likely that a whole bunch of data will
suddenly be written to the file all at once.

If you need to have the output committed to disk sooner,
there are at least two things to try:

- Use fflush(fd) after each output operation. This
tells the library to empty any buffers the file might
be using, sending their contents to the operating
system.

- Use setvbuf(fd, NULL, _IOLBF, BUFSIZ) immediately after
the fopen(). This tells the library to send data to the
operating system as soon as each line is completed (that
is, every time a '\n' character is written). For even
more immediacy (but perhaps less efficiency) you could
use _IONBF as the third argument.

Either of these will probably solve your problem. However,
there are no guarantees! Different platforms have different
I/O capabilities, and the C language Standard doesn't attempt
to regulate all their complexities. For example, you can be
sure that the library sends the data to the O/S, but you are
then at the mercy of whatever the O/S decides to do with it --
and this is a matter outside the scope of the C language. The
actions of fflush() and setvbuf() are "suggestions," really, not
commands. The suggestions are strong, but less than absolute.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 29 '06 #3
Eric Sosman <es*****@acm-dot-org.invalid> wrote:
va************@gmail.com wrote:
<...>
This is done a by a thread in the server. Its a while(1) loop, so the
file never closes. The file "file.log" opens in the directory but never
writes anything into it.

<...>
Different platforms have different
I/O capabilities, and the C language Standard doesn't attempt
to regulate all their complexities. For example, you can be
sure that the library sends the data to the O/S, but you are
then at the mercy of whatever the O/S decides to do with it --
and this is a matter outside the scope of the C language. The
actions of fflush() and setvbuf() are "suggestions," really, not
commands. The suggestions are strong, but less than absolute.


In addition to that, some file systems do
not update the file information structures
until the file is closed.

It is possible that the data was actually
written to disk, but the file size will
still be zero for any other process
attempting to read it.

You may have to close the file periodically
and then reopen and append data to it, or
implement a rotation mechanisms where the
log file is closed after reaching a size
or number of entries limit, and a new one
is created to continue logging data.

As mentioned before, these issues are
platform specific and off-topic for
comp.lang.c
Roberto Waltman

[ Please reply to the group,
return address is invalid ]
Jan 29 '06 #4

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

Similar topics

1
by: David Li | last post by:
I am having a lot of problem with following code. To start with I have a working sets of code and the top level SystemC code looks like this: ----------working main.cpp start here...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
10
by: pjlsr | last post by:
It's close to twenty years since I used the C language and at that time I was doing only floating point computational work, nothing with strings or reading files. I tried to use fopen in the...
11
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly...
5
by: eoindeb | last post by:
I am trying to create a directory on Solaris using the mkdir() function. This works fine when I pass a string literal ("/etc/hosts") to mkdir, but if I try passing a directory pointer to mkdir, it...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
4
by: peter.hrdy | last post by:
Hi guys. i have big problem with using fopen, fsockopen or curl. if i tried to use it on remote site it doesn't works. i've readed to much posts but didn't found anything helpfulll. fopen just...
18
by: Scott | last post by:
Hi, a problem with this following code is really bugging me. tform = fopen(country, "r"); fseek(tform, 9L, SEEK_SET); fgets(player2, 38, tform); printf("Player Name (save): %s", player);...
16
by: Jm.GlezdeRueda | last post by:
Hi all, Im trying to read a 24bit bmp with fread, and i have some problems.. I want to read the whole structure in one time, but i dont know why, it only reads the first member well.. I have...
3
by: IamtheEvster | last post by:
Hi there, I'm using fopen for the first time and I know I'm running into a permissions problem, but I can't seem to resolve it and any help would be greatly appreciated. I'm running PHP5 and...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.