Difference between files written in binary and text mode in C++


Text modeBinary mode
In text mode various character
 translations are performed i.e;
“\r+\f” is converted into “\n”
In binary mode, such translations
are not performed.
To write in the files:
ofstream ofs (“file.txt”);
Or
ofstream ofs;
ofs.open(“file.txt”);
to write in the files:
 ofstream ofs(“file.txt”,ios::binary);
or
ofstream ofs;
ofs.open(“file.txt”, ios::binary);
To add text at the end of the file:
Ofstream ofs(“file.txt”,ios::app);
or
ofstream ofs;
ofs.open(“file.txt”, ios::app);
To add text at the end of the file:
Ofstream
ofs(“file.txt”,ios::app|ios::binary);
or ofstream ofs;
ofs.open(“file.txt”, ios::app|ios::binary);
To read files:
ifstream in (“file.txt”);
or
ifstream
in ; in.open(“file.txt”);
To read files:
 ifstream in (“file.txt”, ios::binary);
or
ifstream in ;
in.open(“file.txt”, ios::binary);

Updated on: 30-Jul-2019

466 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements