what is the difference between binary and txt modes in C++

1.2k views Asked by At

I began to use C++ recently,and this may seem to be a nieve queation but I couldn't find an answer for it. When creating an fstream object, I have two options for mode, binary and txt. fstream f ("file.txt",ios::out|ios::binary); and fstream f ("file.txt,ios::out|ios::binary); both write the same strings when use the overloaded operator << . my question what is the differwnce between the two modes and does it affect the number of bytes used to write characters to the stream, so you will need a diifferent seekg when you read data written with each fstream ?

1

There are 1 answers

0
Olivier Poulin On

Certain special characters may get changed depending on what mode you are using. Also, what those special characters get changed into may depend on the OS or computer system that the code runs on.

With binary files you are SURE that the file will be read as-is, on any computer and regardless of the contents of the file. The difference in the kind of file IO says it all: Text mode is for text based files, Binary is for all other kinds of IO (even text files if you don't want any interpretation to take place!)