![]() |
|
#1
|
|||
|
|||
fflush()?Dear friends,
I am wirting a program that involves a great deal of writing to files. In order to constantly checking the file size , I have included a lot lines of fflush(), so that I can use ftell(File) in the debug window to monitor the size of the file on the fly. I am not sure if this is good practice, is there a down side of using fflush so many times? Thanks in advance for your replies! Best regards, Dave |
|
#2
|
|||
|
|||
|
fflush flushes the buffer and forces a write to file.
You can use unbuffered write calls to avoid needing to rely on a call to fflush. However, the reliability will come at the cost of what the buffering provides. |
|
#3
|
|||
|
|||
|
Speaking generally here: The less bytes you write between each fflush() the slower your program is. fflush() is best used only before closing the file to ensure that everything from the write buffer gets sefely to the file.
The file IO operations are relatively slow operations (e.g. compared to memory) involving seek time, write speed and read speed. Therefore it's better to use buffers to make IO more effective. A larger chunk of bytes gets written once. If every small piece of data was to be written separately it follows that for every piece the harddisk spends time in searching the correct place to continue the writing (==seek time). And that is exactly what happens if you force the system with repetitive use of fflush(). If you are appending to an existing file then you can use ftell() once right after opening the file so that your program knows the initial size of the file. Then you should keep a counter variable which you increase by the amount of written bytes every time you write to the file. It would be also interesting to know what exactly your program needs to monitor the filesize for. Is it because of targetting for some limitation like floppysize 1.44 MB ? If so then keep in mind that you need to take into account some overhead for the actual size in the filesystem. One reason is the allocation unit size used by the filesystem. Last edited by maprich : 07-Sep-2005 at 11:07.
Reason: edited poor language
|
|
#4
|
||||
|
||||
|
Quote:
Quote:
Generally, this is very true. But if your data is critical, and losing some because of a problem would be a disaster, the output should be written as soon as it's available. Quote:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#5
|
|||
|
|||
|
Dear friends,
Thanks a lot for all your responses. The reason I want to examine the file size is I want to fit the generated stream into packets in order to transmit over the Internet. It seems now that inserting a counter is a better way. I think I will go from here, thanks again guys! David |
Recent GIDBlog
Prepping for deployment by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Things to Avoid in C/C++ -- gets() , Part 1 | WaltP | C Programming Language | 5 | 21-Jun-2007 12:13 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 21:07 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The