ID:276524
 
Im doing a windows application in C++. I was woundering how I handle binary files. Also I was woundering how I handle byte's after I get them from the binary files. Do I just convert them too ints?
Can the Win32 API help? The only functions I could find were the CreateFile(),WriteFile(),and ReadFile(). Right now Im storing the stuff I get from the file in a BYTE array. But Im not sure if that is binary, or how to handle that type of data type?

Would I just handle them like this?
BYTE Bf[11];
// Get data from file and store in Bf
if(Bf[0]==1)
// The first byte is a 1?


I read about handling binary files with the fstream in regular C++. But Im not sure if that's only for consoul, or if it would work with window apps too.
You can use either the Win32 API or C++ streams. (The C++ functions are actually just wrappers for the Win32 API anyway - I know this because one of my experimental projects relies on this fact. Don't ask. =))

There's no real difference between a text file and a binary file, technically speaking. Distinctions are sometimes made for various purposes; how the file is displayed, perhaps (e.g. TextPad displays normal text for text files but turns into a hex editor for binary files) or how line endings are treated (e.g. in Python you have to specify if you're opening a binary file, so that it doesn't auto-convert line endings to Unix format).

Both the Win32 API and C++ streams give you the raw binary data without doing any translations, so don't worry about it. How you're doing it is fine. (But beware of buffer overflows, of course; if your buffer holds 11 characters, don't ever read more than 11 characters into it, or you will die a painful painful death.)
In response to Crispy
Crispy wrote:
The C++ functions are actually just wrappers for the Win32 API anyway - I know this because one of my experimental projects relies on this fact. Don't ask. =)

So that part is not platform independent?

Also Crispy, where do you go to find information on all this stuff? If only there was a site that listed all the major libraries, described them all and had their functions catalogued things would be a lot easier.
In response to Loduwijk
Loduwijk wrote:
Crispy wrote:
The C++ functions are actually just wrappers for the Win32 API anyway - I know this because one of my experimental projects relies on this fact. Don't ask. =)

So that part is not platform independent?

The Win32 API is platform dependent, obviously. On the other hand, the C++ streams will work on (almost) any platform; they're only wrappers for the Windows API when on Windows. They'll still work on Linux/MacOS/whatever.

Also Crispy, where do you go to find information on all this stuff?

Google, mostly. =) Other than that, MSDN is the authoritative reference for stuff specific to the Windows API. There also are dozens of C/C++ reference sites scattered around the 'net that have documentation on various standard libraries. Your internet search engine of choice will find them.
In response to Crispy
Crispy wrote:
Loduwijk wrote:
Crispy wrote:
The C++ functions are actually just wrappers for the Win32 API anyway - I know this because one of my experimental projects relies on this fact. Don't ask. =)

So that part is not platform independent?

The Win32 API is platform dependent, obviously. On the other hand, the C++ streams will work on (almost) any platform; they're only wrappers for the Windows API when on Windows. They'll still work on Linux/MacOS/whatever.

So that means I should stop using the win32 API functions and switch too the C++ streams. Which is good since I fell better handling the C++ streams.

Also Crispy, where do you go to find information on all this stuff?

Google, mostly. =) Other than that, MSDN is the authoritative reference for stuff specific to the Windows API. There also are dozens of C/C++ reference sites scattered around the 'net that have documentation on various standard libraries. Your internet search engine of choice will find them.
In response to Green Lime
Green Lime wrote:
So that means I should stop using the win32 API functions and switch too the C++ streams. Which is good since I fell better handling the C++ streams.

Yup. No sense locking yourself in to Win32 unless you have to. =)
In response to Crispy
heh yea, I use C++ streams over the win32 api whenever I can. As much as I enjoy the win32 api, I sometimes feel like it's 'really' not necessary in certain cases. :)