LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Use fflush for FmtFile

Hello -

 

I am modifying my code to use Fmt/FmtFile/FmtOut because these are likely optimizes for CVI verses fprintf(), sprintf(), etc.  However I have to create variables, such as char buf[512], and concatenate the strings to write out a line of values.

 

What are the other benefits when it is less efficient?

 

- and -

 

Do I need to use fflush() like I did when I used fprintf()?

 

Thanks,

Matthew

0 Kudos
Message 1 of 3
(3,387 Views)

matth3w6247 wrote:

Hello -

 

I am modifying my code to use Fmt/FmtFile/FmtOut because these are likely optimizes for CVI verses fprintf(), sprintf(), etc.  However I have to create variables, such as char buf[512], and concatenate the strings to write out a line of values.

 

What are the other benefits when it is less efficient?



 I'm not sure to understand what you are saying: can you post some code snippet to help discussing this argument?

 

 


matth3w6247 wrote:

Do I need to use fflush() like I did when I used fprintf()?




You cannot use fflush on a file opened with OpenFile, as the file handles have completely different structure and cannot be shared between the two commands.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 3
(3,377 Views)

i don't think that Fmt/FmtFile/FmtOut are expecially more optimized than their standard C counterparts. remember that the functions which are part of the C standard are implemented in the C runtime, which is provided by NI through cvirte.dll. we can think NI engineers to optimize the Fmt*() function as well as the *printf(). (anyway, i bet Fmt*() is implemented in terms of *printf()...).

 

now you said:

"Do I need to use fflush() like I did when I used fprintf()?"

 

fprintf() do not "need" a call to fflush() unless you absolutely want the data to be written to the file right now. when using fprintf(), the data are stored in a system dependent buffer. this buffer is written to the file when there is enough data to write (usually a full disk cluster, that is 4KB) or when closing the file. note that this behavior is identical when using fwrite(). writing the buffer is a very slow operation, especially when writing very small chunks of data as the overheadof computing the position of the data on the disk becomes significant. fflush() forces this buffer to be written to the disk, and thus is a real performance killer: i have seen applications slowed down 10 times because of the inconsiderate use of fflush(). however, it is sometimes necessary to have a crash proof writing, but then the only choice is to exchange speed with safety and call fflush() on every write. note that windows is kind of smart: when the application crashes, it closes file handles gracefully and tries to write pending data to the disk. so fflush() is only really necessary when you fear a fatal system crash or a power failure. (in a sense, fflush() is equivalent to closing and reopening the file...)

Message Edited by dummy_decoy on 07-23-2009 11:00 AM
0 Kudos
Message 3 of 3
(3,376 Views)