Register and join over 40,000 other developers!
Recent Topics
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
-
Job Gig PHP Form Needed
PJohnson - Apr 18 2019 03:55 AM
-
How to make code run differently depending on the platform it is running on?
xarzu - Apr 05 2019 09:17 AM
-
How do I set a breakpoint in an attached process in visual studio
xarzu - Apr 04 2019 11:47 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

9 replies to this topic
#1
Posted 14 December 2011 - 07:57 PM
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?
Can somebody explain this to me?, are all streams in c buffered?
#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
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?
Also tagged with one or more of these keywords: fflush, stream
Language Forums →
PHP →
Save JPG image every second - but avoid saving damaged/corrupt images comprised of multiple images?Started by elliottveares, 24 Nov 2015 ![]() |
|
![]() |
||
Language Forums →
C# →
(C#) How to capture video stream without using a third party SDKStarted by BaumHard920, 17 Nov 2014 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
QDataStream: Open QString Values in geditStarted by untitled_1, 16 Feb 2013 ![]() |
|
![]() |
||
General Forums →
General Programming →
Continuous streaming to audio clips.Started by Roman Y, 12 Dec 2012 ![]() |
|
![]() |
||
General Forums →
Everything Else →
Request Services →
Reading Id3 Tags from A SHOUTcast mp3 Radio Stream with PHPStarted by HasanKeyf, 01 Nov 2012 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download