files
Yep. Doesn't matter what language you're using.
Everything on disk is in "binary", including text. The distinction between a "text file" and a "binary file" is not particularly solid, and some not all OSes handle them differently, anyway.
Usual expectations on a "text file" are:
- some character like control-D or control-Z may be expected to indicate the end of the file.
- depending on platform, either CR, LF or the CR+LF sequence is interpreted to indicate the end of a "line of text".
- (less commonly insisted upon) the last line should end with such a CR, LF or CR+LF sequence.
Opening a file in "text mode" is just used to ensure that those conventions are observed. This means that if you try to read something as a text file that was "meant" to be read as binary, then some characters may be inserted or removed (whereever there is a byte with value 10 or 13 in the file, it may be subject to "translation" from one newline encoding to another), and it might be truncated (at the first byte with value 5 (ctrl-d) or 27 (ctrl-z) in the file).
Opening a file in "binary mode" ensures that you see every byte of the file, exactly as it is recorded on disk. If you open someone else's "text file" that way, it might look double-spaced, or all run together on one line.
Everything on disk is in "binary", including text. The distinction between a "text file" and a "binary file" is not particularly solid, and some not all OSes handle them differently, anyway.
Usual expectations on a "text file" are:
- some character like control-D or control-Z may be expected to indicate the end of the file.
- depending on platform, either CR, LF or the CR+LF sequence is interpreted to indicate the end of a "line of text".
- (less commonly insisted upon) the last line should end with such a CR, LF or CR+LF sequence.
Opening a file in "text mode" is just used to ensure that those conventions are observed. This means that if you try to read something as a text file that was "meant" to be read as binary, then some characters may be inserted or removed (whereever there is a byte with value 10 or 13 in the file, it may be subject to "translation" from one newline encoding to another), and it might be truncated (at the first byte with value 5 (ctrl-d) or 27 (ctrl-z) in the file).
Opening a file in "binary mode" ensures that you see every byte of the file, exactly as it is recorded on disk. If you open someone else's "text file" that way, it might look double-spaced, or all run together on one line.
Asksalot here::
So there must be some kind of way the OS determines under which format the files are to be read ( like a set of bytes at the beginning of the file)??
So there must be some kind of way the OS determines under which format the files are to be read ( like a set of bytes at the beginning of the file)??
No, the OS does know nothing about it.
It is your responsibilty to decide wether you want to open your file in text or binary mode.
Thermo
Damn, the Mods always bring the point over much crispier :)
[Edited by - Konfusius on September 29, 2004 9:19:29 PM]
It is your responsibilty to decide wether you want to open your file in text or binary mode.
Thermo
Damn, the Mods always bring the point over much crispier :)
[Edited by - Konfusius on September 29, 2004 9:19:29 PM]
Askalot here::
I dont mean that.I mean when you write a file.Since all files are in essence binary,there has to be some way for the OS to tell which type is which.When you write a file in text mode,it cant just be read as binary right??And vice-versa.How's that done?
I dont mean that.I mean when you write a file.Since all files are in essence binary,there has to be some way for the OS to tell which type is which.When you write a file in text mode,it cant just be read as binary right??And vice-versa.How's that done?
The OS doesn't care. It is your software that cares. Your software is what "reads" it in text mode, which just means there is an extra translation layer IN YOUR SOFTWARE.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement