Thread: fflush(myFile)

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    fflush(myFile)

    Hi Guys,

    After getting some brilliant replies about fflush(stdout), I would like to get some details about flushing a file.

    When Should I fflush a file when writing stream to a file when I use one of the functions, fputc , fputs, fprintf, fwrite...... ??

    Many thanks for your help!!!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In general, you don't need to worry about flushing files. Files are automatically flushed when the buffer gets full, or when you close the file with fclose().

    You may want to flush the file if you know your program is going to crash, for example, and some data in the buffer might be lost. Or if two programs are accessing the same file and the data needs to be put into the file. Or whatever -- but you don't have to flush files that often.

    Also see this page: cpwiki.sf.net/fflush
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Thank you!!

Popular pages Recent additions subscribe to a feed