Steven Lu
Steven Lu

Reputation: 43427

Different ways of Opening a file in C++ on Windows?

When using fopen or fstream, it's necessary to specify binary mode in order to prevent character conversions. On UNIX like systems this isn't usually necessary but on Windows it is.

This leads me to wonder if this is still the "natural" way to access filesystem data on this operating system. UNIX has the "everything is a file" architecture going for it, so is there some windows equivalent of a lower level device to access the data? After all, if there are character conversions going on there's the overhead involved in checking each character to see if it needs to be converted. Hopefully opening a file in binary mode would simply disable any such processing but I have my doubts.

Could anybody shed some light on this?

Upvotes: 4

Views: 1865

Answers (3)

John Leidegren
John Leidegren

Reputation: 60987

the C run-time libraries does what the C run-time libraries does.

Windows actually has most things as a file (or should I say objects) a file name in Windows is actually an object in the global namespace \??\. If you look up the documentation for CreateFile on MSDN there's a lot more to it, especially if you go digging through the Windows kernel documentation.

If you're curious about objects on Windows, you can also download and run WinObj from technet.

Depending on what type of project your working on, I'd put an abstraction layer around IO just to be able to fully utilize what's in Windows but not in the C run-time libraries. I rarely write code for different platforms not going through the native API on each platform (or through some intermediary abstraction).

Upvotes: 1

i_am_jorf
i_am_jorf

Reputation: 54600

Yeah, there are Win32-specific functions. Purists will say it makes your code non-portable. And it does! But who cares, you're writing a Windows program.

OpenFile, CreateFile, etc.

Upvotes: 1

Goz
Goz

Reputation: 62323

You could just use windows file handles. CreateFile, ReadFile and WriteFile.

This is pretty similar to the Unix way of doing things. Lots of things can be done using these functions. Not sockets mind but its about as close as you'll get!

Upvotes: 2

Related Questions