I am unsure about what the purpose of ios::binary is. Can someone please explain this to me. I ask because I need to write a program that keeps students' attendance. The program needs to create a file, and then be able to change the contents of the attendance entries. I'm recording the data in an array of structures. I know how to create/or reopen the file and initial entries using:
fstreamobject.open("nameoffile.txt", ios::in | ios::out | ios::ate);
but I am stuck on how to go about changing specific attendance entries.
should I be using seekp and seekg member functions to access individual elements in the array.
or should I have created the file in binary mode and use the write and read member function?
By default, files are opened in 'text mode', which means that newlines are automatically translated - e.g. on Windows, newlines are \r\n but the translation ensures that the program only has to work with \n. This would obviously wreck binary files, so the binary flag disables that translation from taking place.
Binary files are useful when you're writing non-text data, like an array of numbers. You could store everything in text of course, but that would make the file larger and there's CPU time involved in translating to/from text. That array of numbers is a good example: there's actually a fair amount of arithmetic involved in converting from binary to text, including a lot of expensive division.
All files are 'binary files'. There's nothing different from one file to another except the name and contents. The streams in C and C++, however, different between text mode and binary mode - this has nothing to do with the file, it only affects the behavior of he stream.