I'd like to know how a computer can determine the beginning and end of certain file components (attributes, headers, frame/packet/segment headers etc.) when these components can be omitted or added in optionally, on top of being of variable length, and how it can even recognise files and protocol packets within an ocean of 1s and 0s at all.
Part of this questions stems from a theoretical about how you'd go about reading binary data from a storage medium that has been physically broken into multiple pieces. If, say, there was a text file stored within a broken piece, and if a computer could physically read the binary data successfully, could it find the start+end of this text file then read its contents?
I read over the answers provided in this question and this question, which (from my understanding) explains that data is stored in "blocks"/"sectors"/"clusters" of a minimum size based on the file system, and that the OS needs to know the sector associated with the data it needs (to pull up a file, load a program etc.), and the storage medium controller needs to know where this sector is physically loaded in order to read it and send it up the protocol stack. If there was only one file in each "sector" this would make sense, the OS would just read the sector from start to finish to read the file, and the remaining space left in the sector would be wasted space (all 0s) - the intuitive sense breaks down for me if there are multiple files per sector, though.
The other part of this question stems from the fact that an Ethernet frames (and other protocols, for that matter, I just have a background in Network Engineering, hence why I chose Ethernet for this question) can have varying header lengths - between 14 bytes at base, 18 if a single 802.1Q tag is used, and up to 22 bytes if 802.1ad tagging is used. Including or omitting certain optional headers actually changes the amount of bits a computer has to read when receiving the Ethernet frame (it changes the length of the frame header) - how would a computer know when to begin reading the Payload after the Source MAC address ends, if the 802.1Q tag between these two fields is optional AND of variable length?
If this question should be asked as two separate questions, and/or in different Stack Exchange Sites, please let me know.