Advertisement

Binary File Access

Started by September 19, 2001 05:01 PM
2 comments, last by ATronic 23 years, 5 months ago
Hey, I''ve been programming with c++ for about two years now. I''ve only used ASCII file access using fstream. How exactly do you work with binary files? I know it''s better, smaller, ect., so I figured I should learn. Alex Broadwin A-Tronic Software & Design ----- "if you fail in life, you were destined to fail. If you suceed in life, call me." "The answer is out there." "Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"
I''m not sure in exactly which direction to point you, I guess it''s mostly a matter of opinion.

If you want to use standard C streams, you should look into examples using fopen(), fread, fwrite, etc.

I don''t really know where to start, I don''t know the extent of your knowledge. Try msdn.microsoft.com and look up the above functions. If you''re interested in WinAPI specific functions, also search for CreateFile, ReadFile, WriteFile, and CloseHandle. If you can''t find these on that site, search with google or hotbot

Good Luck

Feel free to email me.
[email=JValentine_13@hotmail.com]contact[/email]
Advertisement
If you have MSDN, you can get a better description than what I''m about to post. Look up "ios::binary".

Basically, when you declare your instance of an ofstream or ifstream (or whatever file class you''re using), you need to specify the binary mode since it defaults to text (as you prolly already know).

#include

ofstream fout("Output.bin", ios::binary);
ifstream fin("Input.bin", ios::binary);

You can write to the file by using the write member function:

generic_class foo;
fout.write( (char *) &foo, sizeof(foo) );

The first argument is a pointer to the first byte of data you want to write to the file. The second argument is the number of bytes that are written from that address. NULL characters do not stop the function, so if you use sizeof, your entire class (or whatever) will be written competely to the file.

You can read from a file by using the read member function:

generic_class foo;
fin.read( (char *) &foo, sizeof(foo));

As you would expect, the first argument is the address you wish to load data into, and the second argument is the number of bytes that are written afterwards.

I think that everything else can be done the same way as text files, but then again, it depends on what you want to do. That should be enough to get you started though.

eriol

Thank you,
exactly what I needed! Now I can get back to coding. I never knew fstream was able to write binary. Thanks a ton.

Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
Alex BroadwinA-Tronic Software & Design-----"if you fail in life, you were destined to fail. If you suceed in life, call me.""The answer is out there.""Please help, I'm using Windows!"

This topic is closed to new replies.

Advertisement