Jump to content

File streams C

- - - - -

  • Please log in to reply
9 replies to this topic

#1
charles-eng

charles-eng

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
Hello, I've been working with files using c and I've noticed that when I write to a file the data doesn't go directly to the file but it's buffered and it's only writen when I close the stream or when I use fflush. That's a little shicking to me because I thought only streams declared to be buffered with setbuf() were actually buffered.

Can somebody explain this to me?, are all streams in c buffered?

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Here's what I'm thinking: it is always buffered, but using setbuf lets you allocate your OWN buffer to a file object. I'd think it is done this way because it is much more efficient to do a few large writes to a file than it is to do a bunch of small writes.
Latinamne loqueris?

#3
charles-eng

charles-eng

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts

mebob said:

Here's what I'm thinking: it is always buffered, but using setbuf lets you allocate your OWN buffer to a file object. I'd think it is done this way because it is much more efficient to do a few large writes to a file than it is to do a bunch of small writes.

Thanks for your answer, it makes a lot of sense since writting to the hard disk is a very expensive operation.

#4
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
You can disable buffering by calling setbuf() with a NULL buffer parameter.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
But it wouldn't make sense for the operating system to use buffering, though, I mean, doesn't it already use swap file(/s), so wouldn't the operating system end up writing the buffer to the disk via virtual memory anyway?

#6
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
Buffering actually started out many years ago with MS-DOS version 1 when the os did not use swap files. Use of swap files is much different than buffering -- swap files may or may not contain the data that is buffered.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I think hard disk drives were slower back then, too.

#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
@RhetoricalRuvim writing to a hard drive is still a lot slower compared to writing to memory. Doing larger writes is less expensive than doing groups of smaller ones. Anyway, the minimum size you can read/write is 512 bytes, so writing just a few bytes at a time would be extremely inefficient.
Latinamne loqueris?

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Yeah, I suppose so, but still, why buffer and not write to the disk, if you'll end up writing to the disk anyway? But it would make sense if the data is less than 512 bytes in size.

The other thing, is it 512 bytes for all drives, or just floppy disk and hard disk drives?

#10
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Lets say you were writing to a 512 byte block of hard drive space, but you were doing it, say, 16 bytes at a time. If you weren't buffering, it would make 32 writes to the hard drive as opposed to 1 if you were buffering.

Most hard drives and all floppies I know of use 512 byte sectors. As for other stuff I don't know.
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users