files

Started by
17 comments, last by GameDev.net 19 years, 6 months ago
By essence,all file types can be read has binary files right?( in C++).
Advertisement
yes.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
I liked my answer better. [grin]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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)??
Nope

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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]
Konfusius: I think Washu likes *his* answer better too. :D
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?
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement