How does fstream work under the hood?

How exactly does it read from the hard drive? I Googled for some time, but couldn't find any solid answers. I read somewhere that it wraps platform specific methods, but I'm not sure about the source.

I'm asking out of curiosity, not because I'm reinventing the wheel:)
At the most basic level computer are all 0's and 1's which translate loosely to those transistors on-off on the mother board. Long long time ago before even assembly language was invented, "programming" was mechanical http://en.wikipedia.org/wiki/Charles_Babbage

Then as time evolves, mechanical turn to assembly which code against an instruction set that translate to the underlying "mechanical parts". And I think the code is printed on punched cards ? Feed into computer to process ? Then the code becomes printed on text file later on.

Then as time evolves again, assembly give way to higher level programming language like C which form a layer on top more human readable so programmers can program against that which in turn go to assembly which in turn to computer processing.

I hope my understanding is correct but that is how I view. Abstraction layer by layer is happening to computer programming throughout the years. How will it be 50 years down the road? We program using our brain to think? :P
So hand coded assembly is used to connect/read from the hard drive (or any other memory device)? Is this done through some asm instructions not directly available in higher level languages?
I believe all C++ compilers come with the STL source code. You can just examine it.
We program using our brain to think?

Wouldn't it be nice to program without having to even think? :)

http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01161_source.html

After digging you get to a function sputn() (for writing) but I couldn't find the source for it. Anyone else?
I read somewhere that it wraps platform specific methods.


I think they have to eventually.

I can see the Dinkumware STL's <fstream>, provided with VC2008, calling fwrite, fputc, etc.

fwrite -...-> _write -...-> WIN32 WriteFile


And it calls _fsopen (rather that fopen) which, a number of layer into the CRT, calls WIN32 CreateFile.
Last edited on
closed account (zb0S216C)
How about this:
http://home.teleport.com/~brainy/diskaccess.htm

Wazzak
Well, even DOS apps didn't use int 13h to access the disk, due to it's limitations.

A very primitive system could take that route, but I don't think (e.g.) embedded programmers tend to use C++ (aren't they more inclined to C?)

And, of course, no modern operating system could take such a route.
Last edited on
C++ streams actually do their I/O through an underlying streambuf. The streambuf's get and put methods are those pieces of code that are actually pre-compiled for the platform.

Underneath, the compiler writer will use either the standard C file routines (which use the native platform file I/O routines) or the native platform file I/O routines directly.
I figured it would eventually have to come down to platform specific stuff...

Thanks everybody for your help!
Topic archived. No new replies allowed.