Binary files

Hello,

When would you use a binary file as opposed to a text file? Why do you return -1 if a file cannot be opened? Also, if you are working with a binary file with flags ios::out | ios::in would you need to close the file before switching between .read and .write?

Thanks.
Last edited on
You use a binary file when a text file doesn't fit your needs.
You don't have to return anything if you cannot open a file. This is entirely application-specific, unless I misunderstood.
No, you don't need to close a file to go from reading to writing if you open it with ios::in | ios::out, as far as I know (I usually don't use filestreams, sorry). The best would be for you to do a test, wouldn't you say? A simple test should take 10 minutes at most. What I do believe you need to do is to make sure the file pointer is where you want it.
Thanks webJose.
1) Binary files can store the same data in less space than text files.
2) Binary files can have their own editors (e.g. Photoshop for images, SoundForge for audio, etc), so most people won't edit them "manually" (e.g. with a hexeditor or Notepad) and corrupt them. This means you can expect your binary files to keep a strict format, and you can't expect this from text files.
3) Most people won't bother trying to decipher how you store your information if you provide no editor.
Topic archived. No new replies allowed.