![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 10
| Controlling variable values by printing on file 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 | |
| | #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 | |
| | #3 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is online now | |
| | #4 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- 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 | |
| | #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 | |
| | #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 | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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 | |
| | #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 | |
| | #9 |
| Registered User Join Date: Dec 2008
Posts: 10
| I used fflush(). Thank you very much for your advices... bye! |
| p3rry is offline | |
![]() |
| Tags |
| fprintf, output |
| Thread Tools | |
| Display Modes | |
|
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 |