Basics of Computer Engineering
File Structures: Fields
It is common knowledge that computers and computing devices often need to save data which is located in the random access memory (ram) to a physical file, often in a hard drive. Today, we’re gonna take a look at how bytes can be stored as a stream of bytes in a physical file.
If you write a simple code in python or C, for example, to save these strings (which represent some random names and their telephone’s country code, for example):
what you might end up getting is a file which will most likely look like this:
So, it is quite clear we’ve lost our file structure. How do we solve this problem?
We use fields.
Fields are logical structures that computers use to organize their understanding of the data they are handling within a text file, for example. So, one simple way to solve our problem would be to simply state how many spaces our fields would have. For example, we could state that each of our fields will now have a lenght of 10 characters. So, for each of our text fields, we would allocate 10 bytes of space to hold them. For example, it would most likely look like this:
(here, each * represents an allocated space in our text file with the lenght of 1 byte)
So, it would be quite simple for us to implement a function to read 10 characters, and each of them would be 1 of our original information. So, what is the downside of this approach?
It wastes way to much space in our disk
For each of our fields in this example, at least 6 bytes of information are not being used. Of course, in most cases this is not desirable.
A second approach that solves this problem is to say to the computer how many bytes of space each field will occupy. We could do something like this:
4Theo 3+55 4John 3+81 4Mary 3 +34
(the spaces are for a matter of reading only)
The advantage of using this technique is that notwithstanding we do not waste space, we also need to use a few bytes more to indicate the lenght of the fields if they’re less than 256 bytes long.
Another very common technique is to use a key symbol to tell the computer that it reached the end of a specific field. For example, we could do something like:
When working with this technique, one should always take care of the character being used as a key symbol. It must be one that is not going to be needed by the user, such as in most cases, “|”.
Finally , there is one another method which is used to store data in fields. One could use a “keyword = Value” approach to store the values. How does that work? It is also quite simple, and in our example , it would look like this:
The downside here is that we utilize a lot of bytes in order to indicate which field we are referring to. Here, in most cases, we used more space to indicate which field comes next than to store it’s data.
That’s it folks, see you soon.