ive been reading about this topic and didnt get the specific info for my question :
(maybe the following is incorrect – but please do correct me)
Every file( text/binary) is saving BYTES.
byte is 8 bits hence max value is 2^8-1 = 255 codes.
those 255 codes divides to 2 groups:
0..127 : textual chars
128:..255 : special chars.
so binary file contains char codes from the whole range : 0..255 ( ascii chars+special chars).
1 ) correct ?
2) NOw , lets say im saving one INT in binary file. ( 4 byte in 32 bit system)
how does the file tells the progem reads it : its not 4 single unrelated bytes but an int which is 4 bytes ?
No, a binary file just contains bytes. Values between 0 and 255. They should only be considered as character at all if you decide to ascribe that meaning to them. If it’s a binary file (e.g. a JPEG) then you shouldn’t do that – a byte 65 in image data isn’t logically an ‘A’ – it’s whatever byte 65 means at that point in the file.
(Note that even text files aren’t divided into “ASCII characters” and “special characters” – it depends on the encoding. In UTF-16, each code unit takes two bytes regardless of its value. In UTF-8 the number of bytes depends on the character you’re trying to represent.)
The file doesn’t tell the program. The program has to know how to read the file. If you ask Notepad to open a JPEG file, it won’t show you an image – it will show you gibberish. Likewise if you try to force an image viewer to open a text file as if it were a JPEG, it will complain that it’s broken.
Programs reading data need to understand the structure of the data they’re going to read – they have to know what to expect. In some cases the format is quite flexible, like XML: there are well-specified layers, but then the program reads the values with higher-level meaning – elements, attributes etc. In other cases, the format is absolutely precise: first you’ll start with a 4 byte integer, then two 2-byte integers or whatever. It depends on the format.
EDIT: To answer your specific (repeated) comment:
Either the program reading the data needs to know the meaning of the data or it doesn’t. If it’s just copying the file from one place to another, it doesn’t need to know the meaning of the data. It doesn’t matter whether it copies it one byte at a time or all four bytes at once.
If it does need to know the meaning of the data, then just knowing that it’s a four byte integer doesn’t really help much – it would need to know what that integer meant to do anything useful with it. So your file written from the command shell… what does it mean? If I don’t know what it means, what does it matter whether I know to read one byte at a time or four bytes as an integer?
(As I mentioned above, there’s an intermediate option where code can understand structure without meaning, and expose that structure to other code which then imposes meaning – XML is a classic example of that.)