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?
9 replies to this topic
#1
Posted 14 December 2011 - 07:57 PM
|
|
|
#2
Posted 14 December 2011 - 08:12 PM
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
Posted 15 December 2011 - 08:04 PM
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
Posted 15 December 2011 - 09:14 PM
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
Posted 15 December 2011 - 09:45 PM
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
Posted 16 December 2011 - 06:26 AM
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
Posted 16 December 2011 - 11:29 AM
I think hard disk drives were slower back then, too.
#8
Posted 16 December 2011 - 04:21 PM
@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
Posted 16 December 2011 - 04:27 PM
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?
The other thing, is it 512 bytes for all drives, or just floppy disk and hard disk drives?
#10
Posted 16 December 2011 - 04:47 PM
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.
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


Sign In
Create Account


Back to top









