help with fwrite()

Discussion in 'C++' started by T. Crane, Feb 1, 2008.

  1. T. Crane

    T. Crane Guest

    Hi all,

    I'm trying to use successive calls to fwrite() to write out to a file.
    I don't have any trouble opening the file or getting everything I need
    in there, but I do notice a peculiarity about it that I will describe.

    I call it like so:

    fwrite(&buffer, 14, 1, filestream)

    I then call it may times in a loop where I've set my buffer pointer to
    change with each iteration so that it writes the data I want it to
    write. When I go into debug mode, I can then open up the file in a
    reader and watch it grow, so to speak. The odd thing is that it
    doesn't seem to grow with every call to fwrite(). It only seems to
    grow in 4 kB jumps. Does anyone know why this would be?

    FYI - I'm using Windows XP and my compiler is Visual C++ 6.0.

    thanks in advance,
    trevis
     
    T. Crane, Feb 1, 2008
    #1
    1. Advertising

  2. * T. Crane:
    > Hi all,
    >
    > I'm trying to use successive calls to fwrite() to write out to a file.
    > I don't have any trouble opening the file or getting everything I need
    > in there, but I do notice a peculiarity about it that I will describe.
    >
    > I call it like so:
    >
    > fwrite(&buffer, 14, 1, filestream)
    >
    > I then call it may times in a loop where I've set my buffer pointer to
    > change with each iteration so that it writes the data I want it to
    > write. When I go into debug mode, I can then open up the file in a
    > reader and watch it grow, so to speak. The odd thing is that it
    > doesn't seem to grow with every call to fwrite(). It only seems to
    > grow in 4 kB jumps. Does anyone know why this would be?


    Buffering.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Feb 1, 2008
    #2
    1. Advertising

  3. T. Crane

    Pete Becker Guest

    On 2008-02-01 18:19:18 -0500, "T. Crane" <> said:

    >
    > I'm trying to use successive calls to fwrite() to write out to a file.
    > I don't have any trouble opening the file or getting everything I need
    > in there, but I do notice a peculiarity about it that I will describe.
    >
    > I call it like so:
    >
    > fwrite(&buffer, 14, 1, filestream)
    >
    > I then call it may times in a loop where I've set my buffer pointer to
    > change with each iteration so that it writes the data I want it to
    > write. When I go into debug mode, I can then open up the file in a
    > reader and watch it grow, so to speak. The odd thing is that it
    > doesn't seem to grow with every call to fwrite(). It only seems to
    > grow in 4 kB jumps. Does anyone know why this would be?
    >


    Output streams are usually buffered by the runtime library, and 4k is
    certainly a reasonable size for that buffer. The contents of the buffer
    only get written to disk when the buffer gets full or you close the
    file.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Feb 1, 2008
    #3
  4. T. Crane

    T. Crane Guest

    OK that's pretty much what I guessed, but I thought I'd ask.

    thanks for the confirmation.

    On Feb 1, 5:33 pm, Pete Becker <> wrote:
    > On 2008-02-01 18:19:18 -0500, "T. Crane" <> said:
    >
    >
    >
    >
    >
    > > I'm trying to use successive calls to fwrite() to write out to a file.
    > > I don't have any trouble opening the file or getting everything I need
    > > in there, but I do notice a peculiarity about it that I will describe.

    >
    > > I call it like so:

    >
    > > fwrite(&buffer, 14, 1, filestream)

    >
    > > I then call it may times in a loop where I've set my buffer pointer to
    > > change with each iteration so that it writes the data I want it to
    > > write. When I go into debug mode, I can then open up the file in a
    > > reader and watch it grow, so to speak. The odd thing is that it
    > > doesn't seem to grow with every call to fwrite(). It only seems to
    > > grow in 4 kB jumps. Does anyone know why this would be?

    >
    > Output streams are usually buffered by the runtime library, and 4k is
    > certainly a reasonable size for that buffer. The contents of the buffer
    > only get written to disk when the buffer gets full or you close the
    > file.
    >
    > --
    > Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > Standard C++ Library Extensions: a Tutorial and Reference
    > (www.petebecker.com/tr1book)
     
    T. Crane, Feb 1, 2008
    #4
  5. T. Crane

    Jalen Guest

    On 2ÔÂ2ÈÕ, ÉÏÎç7ʱ42·Ö, "T. Crane" <> wrote:
    > OK that's pretty much what I guessed, but I thought I'd ask.
    >
    > thanks for the confirmation.
    >
    > On Feb 1, 5:33 pm, Pete Becker <> wrote:
    >
    > > On 2008-02-01 18:19:18 -0500, "T. Crane" <> said:

    >
    > > > I'm trying to use successive calls to fwrite() to write out to a file.
    > > > I don't have any trouble opening the file or getting everything I need
    > > > in there, but I do notice a peculiarity about it that I will describe.

    >
    > > > I call it like so:

    >
    > > > fwrite(&buffer, 14, 1, filestream)

    >
    > > > I then call it may times in a loop where I've set my buffer pointer to
    > > > change with each iteration so that it writes the data I want it to
    > > > write. When I go into debug mode, I can then open up the file in a
    > > > reader and watch it grow, so to speak. The odd thing is that it
    > > > doesn't seem to grow with every call to fwrite(). It only seems to
    > > > grow in 4 kB jumps. Does anyone know why this would be?

    >
    > > Output streams are usually buffered by the runtime library, and 4k is
    > > certainly a reasonable size for that buffer. The contents of the buffer
    > > only get written to disk when the buffer gets full or you close the
    > > file.

    >
    > > --
    > > Pete
    > > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > > Standard C++ Library Extensions: a Tutorial and Reference
    > > (www.petebecker.com/tr1book)


    One thing i want to know is whether there is a way to change the
    buffer size (currently 4K)?

    Thanks
    Jalen
     
    Jalen, Feb 2, 2008
    #5
  6. T. Crane

    Pete Becker Guest

    On 2008-02-01 23:59:45 -0500, Jalen <> said:

    >
    > One thing i want to know is whether there is a way to change the
    > buffer size (currently 4K)?
    >


    Yes, there is. Look it up.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Feb 2, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nescio
    Replies:
    0
    Views:
    452
    nescio
    Dec 21, 2005
  2. nescio

    fwrite() question

    nescio, Dec 21, 2005, in forum: HTML
    Replies:
    0
    Views:
    370
    nescio
    Dec 21, 2005
  3. phil

    fwrite consuming lot of memory help !!!!

    phil, Aug 5, 2005, in forum: C Programming
    Replies:
    7
    Views:
    782
    Lawrence Kirby
    Aug 8, 2005
  4. ern

    fwrite help needed

    ern, Feb 3, 2006, in forum: C Programming
    Replies:
    3
    Views:
    295
    Default User
    Feb 4, 2006
  5. Replies:
    4
    Views:
    634
Loading...

Share This Page