Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

ATronic

Binary File Access

This topic is 6271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!"

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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!"

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!