• Question regarding fwrite() and fflush()

    From Lew Pitcher@21:1/5 to All on Tue Jun 25 18:42:55 2024
    In a recent reply here, I posted some code that used fwrite() to write
    binary data to a file. One version of this code used mmap() to access
    this binary data in-situ, and I wonder if I should have called fflush()
    before calling mmap().

    I understand the where to use fflush() on standard text formatted output
    (where you use fprintf(), etc to externalize the output), but I don't see anything my copies of either the ISO C or POSIX standards that say that
    I /should/ use fflush(), but neither do I see anything that says I /need
    not/ use fflush().

    So, wiser and more experienced minds, should I use fflush() in this case
    or not?

    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Lew Pitcher on Tue Jun 25 13:55:47 2024
    Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

    In a recent reply here, I posted some code that used fwrite() to write
    binary data to a file. One version of this code used mmap() to access
    this binary data in-situ, and I wonder if I should have called fflush() before calling mmap().

    I understand the where to use fflush() on standard text formatted output (where you use fprintf(), etc to externalize the output), but I don't see anything my copies of either the ISO C or POSIX standards that say that
    I /should/ use fflush(), but neither do I see anything that says I /need
    not/ use fflush().

    So, wiser and more experienced minds, should I use fflush() in this case
    or not?

    Yes, do use fflush().

    In the alternative, do a setbuf( file, 0 ) so that 'file' is
    unbuffered.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lew Pitcher@21:1/5 to Tim Rentsch on Tue Jun 25 21:28:24 2024
    On Tue, 25 Jun 2024 13:55:47 -0700, Tim Rentsch wrote:

    Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

    In a recent reply here, I posted some code that used fwrite() to write
    binary data to a file. One version of this code used mmap() to access
    this binary data in-situ, and I wonder if I should have called fflush()
    before calling mmap().

    I understand the where to use fflush() on standard text formatted output
    (where you use fprintf(), etc to externalize the output), but I don't see
    anything my copies of either the ISO C or POSIX standards that say that
    I /should/ use fflush(), but neither do I see anything that says I /need
    not/ use fflush().

    So, wiser and more experienced minds, should I use fflush() in this case
    or not?

    Yes, do use fflush().

    As I suspected.
    Now, my code includes an fflush() after all the fwrite()s are complete.

    In the alternative, do a setbuf( file, 0 ) so that 'file' is
    unbuffered.



    Thanks
    --
    Lew Pitcher
    "In Skills We Trust"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Lew Pitcher on Tue Jun 25 22:54:58 2024
    On Tue, 25 Jun 2024 18:42:55 -0000 (UTC), Lew Pitcher wrote:

    In a recent reply here, I posted some code that used fwrite() to write
    binary data to a file. One version of this code used mmap() to access
    this binary data in-situ, and I wonder if I should have called fflush() before calling mmap().

    Yes.

    Whether the data is buffered or not, is independent of whether the data is
    text or not.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)