C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-16-2008, 11:01 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 10
Controlling variable values by printing on file

Hello to everybody.
I have this simulation that implements some physical system. I get my results on a file using function fprintf. It-s a long Montecarlo simulation that gets one result in about half an hour. I have to collect much results and I write them on the same file.
My problem is:
the file seem to get an update only when the simulation writes about 300 results, so I can't get a satisfying control on the results of my program.
Is there a way to get results more often, or to control my variable in real time maybe (I'm not so used to programming language)?
(I've already tried writing on the file with higher frequency, but I'd like to find a more straightforward way).
Thank you very much
p3rry is offline   Reply With Quote
Old 12-16-2008, 11:11 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Most likely, you would like to use fflush() to force the result out to the file itself. You can call fflush() after every fprintf(), or after a set number of fprintf().

As long as you don't produce a lot of results every second, calling fflush() after each fprintf() should be fine.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-16-2008, 11:14 AM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by p3rry View Post
It-s a long Montecarlo simulation that gets one result in about half an hour. I have to collect much results and I write them on the same file.
My problem is:
the file seem to get an update only when the simulation writes about 300 results, so I can't get a satisfying control on the results of my program.
If one result takes half an hour, 300 results will take 6 days. Why not just write to file every single time?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 12-16-2008, 11:18 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by MK27 View Post
If one result takes half an hour, 300 results will take 6 days. Why not just write to file every single time?
My guess is that the results are printed in bunches of (say) 4KB, which means that each result is about 12 characters (4096 / 300 = 13.6 characters).

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-16-2008, 11:57 AM   #5
Registered User
 
Join Date: Dec 2008
Posts: 10
thank you

I'll try with fflush and then I let you know...
Anyway thank you very much
p3rry is offline   Reply With Quote
Old 12-16-2008, 12:19 PM   #6
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
To control writing behavior to the output file you can use the setvbuf() call on the standard output stream. This can control the write frequency by using a custom buffer size instead of the default BUFSIZ used on Unix platforms.
itCbitC is offline   Reply With Quote
Old 12-16-2008, 12:30 PM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by itCbitC View Post
To control writing behavior to the output file you can use the setvbuf() call on the standard output stream. This can control the write frequency by using a custom buffer size instead of the default BUFSIZ used on Unix platforms.
That's correct. But to the programmer, it often offers MORE control to use fflush(), as it allows a flush to happen exactly where the programmer wants it. If you set a small buffer size with setvbuf(), the risk is that a large number of consecutive writes are being flushed individually, when an fflush() at the end of the sequence allows all of the writes to be done to the internal buffer, and then be flushed as one single write operation. Hence my suggestion to use fflush().

In this particular case, I have a feeling it matters very little, but it's generally a better idea to flush() when some output is complete.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-16-2008, 12:49 PM   #8
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
I suggested setvbuf() only as an alternative since the OP wanted to see the simulation results before the process writes 300 times to the internal buffer. setvbuf() fits better where the op roughly knows how many records or characters to output before moving onto the next iteration. Both routines have their pros and cons though methinks in this case fflush() may very well be the way to go.
itCbitC is offline   Reply With Quote
Old 12-17-2008, 10:09 AM   #9
Registered User
 
Join Date: Dec 2008
Posts: 10
I used fflush(). Thank you very much for your advices... bye!
p3rry is offline   Reply With Quote
Reply

Tags
fprintf, output

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we have vector of vector? ketu1 C++ Programming 24 01-03-2008 05:02 AM
Inventory records jsbeckton C Programming 23 06-28-2007 04:14 AM
C++ std routines siavoshkc C++ Programming 33 07-28-2006 12:13 AM
Simple File encryption caroundw5h C Programming 2 10-13-2004 10:51 PM


All times are GMT -6. The time now is 07:22 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22