Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   IO of binary files (http://www.go4expert.com/forums/io-binary-files-t3490/)

pradeep 20Mar2007 13:45

IO of binary files
To make sure that there is no CR/LF translation on non-Unix computers, you have to use the following lines to open streams to files with binary data.

Code: CPP

ofstream os("output.flt", ios::out | ios::binary);
 ifstream is("output.flt", ios::in | ios::binary);

For Visual C++, when using fstream.h, use in addition the flag ios::nocreate. Otherwise you can open a non-existing file for reading, without complaining. (This is not necessary when using fstream).

DaWei 20Mar2007 13:57

Re: IO of binary files
One should not use the .h versions of header files in C++ when there is a non-h version available. They were provided originally for backward compatibility. If you use, for instance, stdio.h, rather than cstdio, you will not get the protection afforded by the std namespace.

Text mode in windows machines also does more than add the CR; it adds a soft EOF. Some append modes do not deal properly with it. Since all files are binary, anyway, it's best not to use text mode unless you know for a fact that a file you are going to read was written in text mode.

All times are GMT +5.5. The time now is 23:02.