Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
What is the difference between binary and text files in c++? The two programs do not differ between these two modes. why?
Binary Mode:---
C++
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main()
{
    fstream f;
    f.open("E:\\Text1",ios::out|ios::binary);
    f<<"Hello how are you?";
    f.close();
    
    getch();
    return 0;
}

Text Mode:----
C++
#include<iostream.h>
#include<fstream.h>
#include<conio.h>

int main()
{
    fstream f;
    f.open("E:\\Text1",ios::out);
    f<<"Hello how are you?";
    f.close();
    
    getch();
    return 0;
}</conio.h></fstream.h></iostream.h></conio.h></fstream.h></iostream.h>
Posted
Updated 12-Mar-12 19:28pm
v2

This is one of the most confusing arguments between newbies, since the concept of "binary data" is often referred by different publications, with different meanings.

In particular, what the <iostream> library refer to "text" and "binary" mode deals with the "character translation".

A C++ stream is defines as a virtually infinite sequence of symbols named "characters".

In "text mode" certain control characters are interpreted according to the underlying operating system needs. For example, in Windows, the CR character is replaced by a CR/LF sequence.
In "binary mode" no translation is done, and characters are written as they are supplied to the stream itself.

In your case this can be made evident by writing some "\r" and "\n" into your strings, and looking at (with an hex editor) how the resulting files look.


This concept is completely unrelated from another concept, that is the representation of data (numbers, in particular): it may happen either in textual form (the number 123 viewed as the sequence of the glyph '1' '2' and '3' one after the other, each represented by its own code-point) or in some other coding like the binary form, that can be the same as the way the data themselves are represented inside the computer memory (123 = 0111_1011 = 0x7B).

But this is not what you will get by writing numbers with the << operator of an std::ostream: you must use the ostream unformatted methods(put and write) on a binary_mode stream to have a binary-encoded dump of a piece of memory into a file.
Note that, if the stream is in text mode, write works the same, but every byte you dump, will be interpreted and translated accordingly to the text representation rules of the underlying operating system (hence, messing up the representation).
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 13-Mar-12 16:50pm    
Probably you were in a hurry while writing all this: I fixed several cases of plain typo, maybe not all of them. Nevertheless, this answer provides useful explanation which may help (I find it pretty difficult to explain to the absolute novice; please see my answer). So I voted 5.
--SA
Emilio Garavaglia 13-Mar-12 17:40pm    
Thanks.
Fixed some more by me ...


Mohibur Rashid 14-Mar-12 0:53am    
+5
You output just string, how can it be binary or not binary? I think it should resolve your confusion about this very code. The flag ios:binary cannot affect it.

Basically, data is not classified into binary and not binary, because everything is binary. Basically, text is a special form of binary data. The real difference comes into play when you present numeric data in the form of text using decimal or hexadecimal digits and other characters, '0-9', 'A'-'F', '0x', 'E', '-', '+'. The data format using such presentation is usually called "text" format as opposed to "binary", but there is no fundamental classification; again, all data is binary.

—SA
 
Share this answer
 
ascii mode will translate \r\n into \r, while binary won't
 
Share this answer
 
Comments
Ashutosh_g 13-Mar-12 1:35am    
But why we calll the binary file as a binary???
Ashutosh_g 13-Mar-12 1:36am    
If we run the above program then why we can not see the output of the file in binary form?
barneyman 13-Mar-12 1:49am    
all files are stored as 8bit binary - it's just some binary files have text in them and can be easily interpreted as such by the OS and apps
Mohibur Rashid 13-Mar-12 1:56am    
I agree with barneyman,
go to the link: http://stdcxx.apache.org/doc/stdlibug/30-4.html

to understand more

for informaiton of Ashutos, there is no point you will see a data in binary form, you can see in numeric form, either decimal or hexa decimal format but not in binary foarmt
say you want to write number 122 into a file,
if it is in textmode, you can write 3 characters '1','2','2' to represent it
if you write in binary you just need 1 byte. right ?
hopes this helps
jkchan
http://cgmath.blogspot.com
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Mar-12 16:44pm    
I disagree with the vote of 2, because this short answer provides a hint on what is the text mode. And this is not so easy to explain to a complete newbie. (Perhaps the voter of 2 does know this matter herself/himself). I up-voted this answer by 4.
--SA
Sergey Alexandrovich Kryukov 13-Mar-12 17:13pm    
Ha-ha, somebody voted 1 for my answer, too. :-) Now I clearly see that someone extremely ignorant plays here. It happens often :-)
--SA
helperabhi 5-Apr-16 13:40pm    
for 122 as an integer ,there will 4 bytes be used not one.
helperabhi 5-Apr-16 13:43pm    
i give it 1 point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900