r/AskComputerScience • u/deeperactuator • Apr 19 '20
What is the difference between a text file and a binary file? I would assume both will be read the computer eventually as 1's and 0's. How the computer handle them differently?
16
u/Mukhasim Apr 19 '20 edited Apr 19 '20
As far as the machine itself is concerned, all files are "binary files". The difference between "text" and "binary" is more a matter of the programmer's point of view. A text file is expected to follow certain conventions, mainly:
It contains lines that are terminated by a platform-specific newline sequence, which is "\n" on UNIX-like platforms and "\r\n" on MS Windows. (Other endings have been used in the past as well.)
It does not contain the null character '\0'.
A binary file is basically any file that doesn't meet these conventions. In particular, binary files may contain the null character '\0' (a byte with value zero). Many text-processing programs will break if they encounter a null character (zero byte) since it is used by C and other languages to indicate the end of a string and they assume that it never occurs within strings. Programs written to process binary files can't use null-terminated strings, they must keep track of the length of the data instead.
4
u/sentientcabbage Apr 19 '20
This answer refers specifically to Linux and OSX operating systems. I believe that Windows is quite similar, but there are definitely some differences, like extensions (.txt, .exe) having some importance to the machine on Windows.
Your assumption is right! To the machine, it really makes no difference - each file just contains bytes. It's the user who does things with files, though, like opening them in a text editor.
Like another answer here mentioned, text files are written in encodings like ASCII, in which a sequence of 8 1s and 0s (1 byte) map to a specific letter or action (like space or line break).
ASCII encoding only has 128 unique values, however, although each character takes up a full byte, which has space for 256 values. The 256 comes from the number of unique patterns you can make with 8 1s and 0s (more succinctly, this is 28). The size mismatch has to do with how we've built our machines to efficiently chomp up large amounts of uniformly-sized data.
Binary files don't want to waste half their space, so each byte in them uses the full range of 256. As such, when you open one in a text editor, a lot of its contents can't be represented by ASCII.
If a binary file is supposed to be executable, it will also contain an indication to the machine of which tool should be used to interpret it, since - like text encodings - there is no universal language for parsing the 1s and 0s that comprise binary files.
4
Apr 19 '20
Binary files don't want to waste half their space
Using only 128 of the 256 possible value in a byte, you would not use half the space, but 7/8th because 7 of the 8 bits are still used.
In other words: you would need an extra byte for every 7 bytes necessary when using the full 8 bits per byte.
If a binary file is supposed to be executable
Note that text files can be executable as well. In unix environments this usually goes with a shebang, to tell the kernel which program it should start to "run" the text file.
1
u/_Ki_ Apr 19 '20
Note that compiled programs resulting in "text files" can be executable as well.
e.g. this one is executable under msdos:
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
3
u/UncleMeat11 Apr 19 '20
Tom Murphy made a C compiler that compiles to just the printable bytes (almost - it can't handle a few things) for Sigbovik a few years back.
1
3
u/pi_stuff Apr 19 '20
Text files aren't restricted to ASCII; they can use encodings such as UTF-8 or Latin-1, both of which use all 8 bits of each byte.
1
u/antonivs Apr 20 '20
ASCII encoding only has 128 unique values, ...
Tell us more about the encoding systems of yore, grandpa!
7-bit ASCII hasn't been in widespread use since the 1970s.
Even the first Apple II, released in 1977, used all 256 values of a byte to encode characters, as did all the personal computers that came after it, using some form of extended ASCII.
The link touches on the history of this, with the key point being:
When computers and peripherals standardized on eight-bit bytes in the 1970s, it became obvious that computers and software could handle text that uses 256-character sets at almost no additional cost in programming, and no additional cost for storage.
That led to pretty much every manufacturer starting to use 8-bit extended ASCII encodings by the early 1980s at the latest.
2
u/turtle_dragonfly Apr 19 '20
Ultimately, all data on the computer is 1s and 0s, you are correct.
However, we have conventions about how to handle certain collections of 1s and 0s differently than others.
For instance, what makes a file "executable" rather than just data? No reason you couldn't try to execute a text file directly, it just probably wouldn't work.
A bunch of pure data is almost kinda useless without metadata to describe it — answering questions like "is the file executable?" "Is it text" "What encoding is it (if it is text)", etc.
1
u/Bottled_Void Apr 19 '20
In what context?
Some version control systems denote things as text, so when you extract them from version control it provides appropriate line endings (Windows/Unix). Some systems will strip out non-printable characters so assist in compression.
Denoting something as binary usually means, I'm not going to try and open it in some text editor. Don't mess with the contents otherwise you'll break it.
But all this depends on who you've told the file is binary or text.
0
u/NotGoodSoftwareMaker Apr 19 '20
They will be read as 1 and 0 and im pretty sure you could read, write and execute both types and probably even get something useful out.
However what makes the text file different from say a binary, jpeg or gif file or some json file is that they all they have different extensions. These extensions are standard formats which all OS should comply with so that the data contained in these files can be accessed across various platforms. The extensions also instruct the OS how it should interact with the contents of the file, what should be used to read it (so a text editor vs a image preview or a web browser), whether it should be executable and whether it can be written to.
-8
u/Ultimegede Apr 19 '20 edited Apr 19 '20
It different types of binary representation. Google "two's complement"
Edit: please disregard this and read the comment thread instead.
6
u/saudi_hacker1337 Apr 19 '20
This is not even remotely related to op's question. Two's complement is a convenient method for representing signed numbers and doing calculations with them in the processor's level.
-1
u/Ultimegede Apr 19 '20
Yeah my bad. I was trailing away because i was thinking of bits. What i probably should have said was that a text file is actually a binary file, but with a specific order(ASCII) that makes it way more space-efficient than just representing everything seperately as 1 line of numbers. I was trying to say that this happens just about everywhere, and every type of file. With different standards and different optimizations and abbreviations. Although very introvertedly and I can see why it wasn't clear.
1
u/saudi_hacker1337 Apr 19 '20 edited Apr 19 '20
Are you implying ASCII is space efficient? Because it is not the case. It takes you a whopping 4 bytes to represent the number 1000. The single advantage of ASCII is that it is human-friendly (or actually, english-speakers-friendly). And what do you mean by "representing everything separately as 1 line of numbers"? Perhaps I'm misinterpreting your words, like every ASCII parser does. Am I right folks?
Edit: by ASCII I mean representing any file as a simple text file, disregarding any specific format.
1
u/Ultimegede Apr 19 '20
I'm not a native english speaker so I'm sorry if some things gets lost or messed up in translation. I'm not saying it's the most economical way to portray letters. But it is a pretty economical way to handle user input/output. Yes it is possibly the simplest way to be "english-friendly". My language also has non-ASCII characters.
By "1 line" i will try to explain: it can be pretty efficient to store in bits instead of bytes. For example you can take the letter mostly used in the english alphabet and assign "1", so whenever the compiler reads "1" it knows it's an "a". Then the second most used letter "e" will be "01" and "i" will be "010", and so on. I will hope i can skip the rest because of redundancy? Because every word doesn't contain one instance of all letters, there is a very good chance you will save in the long run when assigning this way.
2
31
u/DementedPeople Apr 19 '20
A text file is a binary file but the text file uses an encoding like ASCI that allows us to translate the binary into letters, numbers, and characters.