Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What is the difference between binary file and text file
Posted

Google would have given you an answer more quickly and with less harassment than you're liable to get for asking a question like this here.

Anyway, here's a link[^] to one answer to your question.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Mar-12 15:01pm    
This is a right idea: I find answering such question is awkward. My 5.
Also, you have some point in this "harassment" passage. In fact, the question is natural for the a beginner, first of all, because these terms are confusing. There is nothing wrong about if, except the simple fact that the questions in the form "what is the difference between {0} and {1}" is formally incorrect in principle. By the confusion must be somehow resolved.

It tried to do it in my answer. What do you think?

And you have me the idea to add a link. Maybe Wikipedia article is better.
--SA
Marc A. Brown 16-Mar-12 15:41pm    
Indeed the question *is* natural for a beginner. But experience has shown me that these questions -- the ones that are easily googled or binged -- tend to get the asker flamed to a highly crispy state here on CP, which is unfortunate but unavoidable. That was my only point in bringing it up (I'm sure *you* understand that, but I bring it up in case the OP didn't). See also my comment to your solution. :)
Sergey Alexandrovich Kryukov 16-Mar-12 23:00pm    
Yes, and I appreciate you note and your understanding. In fact, if the question is too "easy" or very elementary, I always afraid to answer... the reason is:....

DISCLAIMER:
Ashutosh_g, please understand that the problems I'm going to describe below are not related to you personally; they are related to SOME OTHER inquirers. I believe you are a decent person trying to learn things well and able to appreciate some help. Your question is not a result of any kind of ignorance, but is the consequence of the fact that the explanations on the matter of your question are really confusing, while you are trying to get really deep understanding. Let me assure you that any your follow-up questions will be quite welcome.

...The reason is that in case of such question, it's too much likely that the answer can spark real scandal. It happens many times already. Usually, if a question is more or less difficult, the inquirer never goes into offensive behavior or plays "offended cards"; such person can appreciate help, such person typically more or less adequately responds even to a hard criticism (either thankfully accepts it or keeps arguing in correct and truthful ways or provide correct criticism), tries to fix things accordingly, asks further questions, etc.

Unfortunately, in contrast, the fact of asking "stupid" (or "GimmeCode", or "answer my test question") questions correlates too well with impudent, offensive behavior, shifting discussion into ethical and personal issues, etc. It happens way too often. (I recently faced with stuff like: making fun of my name (!), accusation in spam/advertizement, comparison of my face with something, all kinds of personal adjectives...)

Do you remember such responses "I'm and the beginner; and you are supposed to help me (!); but you are just lecturing..." Aha, those who ask "GimmeCode" consider lectures as offense... no wonder they need some code as a result... Why wouldn't not they say so to their professors? :-)

It looks like answering "simple" questions is too far from simple... :-)

--SA
This is not a 100% correct question, in fact. In reality: all files are binary. This is a kind of jargon: usually a file is called a text file if does not contain any representations of numeric data at all or such data is represented using human-readable "text mode". This is rather a kind of jargon. Any file can actually contain a mixture of different representation.

Let's consider a 32-bit integer int value = 0x1234ABCD;. In needs minimal 4 byte to write it. (0xCD, etc.). It depends on the data type and does not depend on the value.

At the same time you can write it as a string in hexadecimal form "0x1234ABCD" or in decimal form as "305441741", 10 or 9 characters, and each character will take 1 byte in Unicode UTF-8 or ASCII encoding or 2-byte words in UTF-16 encoding, and the length if representation depends both on the value and the format. For example, compare "12345", "3" and "0003". In the case of such string representation, it's usually called "text format". By the way, the notions of "hexadecimal" or "decimal" are relevant only to such "text format".

There is a number of other forms of representations of numeric and other data. The ways of presentation I have described are used more often, that's why those confusing terms are used. They are really somewhat confusing, many beginners have some troubles with them.

Please see also: http://en.wikipedia.org/wiki/Text_file[^].

—SA
 
Share this answer
 
v3
Comments
Marc A. Brown 16-Mar-12 15:43pm    
Well stated. I understand your point about the question; however, the way it was asked is, in my reading, just a form of shorthand. The OP may be asking while not recognizing your point, but I would probably have asked it the same way since everyone would understand what I was asking for without my attempting to formalize the question.
It *is* good to point it out though.
EDIT: I meant to point out that the link I posted in my answer *did* address the fact that even text files are binary files.
Sergey Alexandrovich Kryukov 16-Mar-12 15:48pm    
I actually agree with all the points you made here, including your idea of a "shorthand" (otherwise I would not answer to OP) and "answer *did* address..." statement (otherwise I would not probably up-vote your answer).

Thank you for justification of my point and for this constructive discussion.
--SA
nv3 16-Mar-12 16:13pm    
Hello SA.

Your answer explained very well the difference between ASCII/Unicode or binary encoded files. From the user's question some 30 min. ago I assumed that the user was experimenting with fstream and hence was interested in the difference between the two read/write modes text and binary. That explains why I answered in the way I did.

Someone must have disliked my answer though and voted me down. That's the tragedy when you are trying to help :-)
Sergey Alexandrovich Kryukov 16-Mar-12 22:31pm    
Actually, it really might be related, as OP tagged C++. Maybe OP should ask a follow-up question related to immediate programming problems; in this case we'll try to clarify on that issue.
--SA
A binary file is treated as raw data and read byte-by-byte. A text file is considered to contain lines of text that are separated by some end-of-line markings. And here is the problem: These end-of-line markings are different depending on the operating system. Unix / Linux etc. use a newline character ('\n'), while DOS and Windows use a two-character sequence of carriage-return plus line-feed (\r\n). By telling the I/O system that a file contains text lines, it can watch out for both types of line separators and deliver the lines to your program in a uniform way. On output it can add the correct line separator to what you write.

Hope that explains it.
 
Share this answer
 
Comments
[no name] 16-Mar-12 20:07pm    
The information you gave is useful and it explains 'binary mode' versus 'text mode' and newline translation quite well.

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