Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

File streams C

fflush stream

  • Please log in to reply
9 replies to this topic

#1 charles-eng

charles-eng

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts
  • Programming Language:C, Java, Assembly
  • Learning:C++, C#, Python, Transact-SQL

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?
  • 0

#2 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

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.
  • 0
Latinamne loqueris?

#3 charles-eng

charles-eng

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 103 posts
  • Programming Language:C, Java, Assembly
  • Learning:C++, C#, Python, Transact-SQL

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.
  • 0

#4 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

Posted 15 December 2011 - 09:14 PM

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

#5 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

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?
  • 0

#6 Ancient Dragon

Ancient Dragon

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 378 posts

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.
  • 0
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#7 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

Posted 16 December 2011 - 11:29 AM

I think hard disk drives were slower back then, too.
  • 0

#8 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

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.
  • 0
Latinamne loqueris?

#9 RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1310 posts
  • Location:C:\Countries\US
  • Programming Language:C, Java, C++, PHP, Python, JavaScript

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?
  • 0

#10 mebob

mebob

    CC Devotee

  • Validating
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

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.
  • 0
Latinamne loqueris?





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download