If it were not for a file, a computer would remain a simple calculator with no backup storage to rely on. Any information stored in a computer is in some form of file. In fact, a keyboard (standard input file), a screen (standard output file), or even a modem is considered a type of file. A program can create, access, or manipulate a file. One kind of file access, where data are accessed one after the other, is known as sequential file access. Alternatively, a file can be accessed here and there randomly at the beginning, at the end or at any spot in the file. This is known as random access. While sequential access is easy and straightforward, it is slow and it does waste memory. On the other hand, random access is faster and does not waste memory; but extra care is necessary to avoid any undesirable effect to the file.
As a file is opened, file indicator (file maker) advances as data is accessed, getting ready for the next operation. A variety of file modes exist, such as ios::in for input and ios::out or ios:app for output. For ios::in and ios::out the file indicator is placed at the beginning. In addition, a restriction can be placed on a file as it is being opened, such as to not create the file, if the file does not exist (ios::nocreate), or to not replace the file if the file already exists (ios::noreplace). Random access can be achieved by using the functions seekp( ) and seekg( ), which position the file indicator at any spot of the file. Therefore, data can be retrieved from or written to the file relative to the current, beginning, or end positions. While by default, files are saved as text files, a file can be created as a binary file, treating the data as it is rather than converting it to a series of text (ASCII) characters. Binary files are better suited for writing and reading data randomly than text files are. With binary files an entire structure was written in one shot by using write( ) function. Similarly, the entire structure was read from the file in the structure by use of read( ) function. One way to apply random access to a file is to fix the size of the data ahead of time. For example, a fixed length must set aside for each record. Using 100 characters (bytes), the 10th record will be placed in the position 1000 to 1099. The use of a key file indicator can speed up random access. However, a wrong key file indicator positioning as a result of bad computation or misplacement may lead to a disaster since the file content will be altered on the spot. There are tradeoffs in all cases.
The content of a file can be dumped into arrays at the beginning and the contents of the arrays stored into the file at the end. Working with arrays is easy and fast. Modification is much easier and deletion can be replaced with a blank or some special value. There may be a problem with too many empty slots in an array when there are several deletions. Also, allocating a huge array in a large database is not logical or, in some cases, there is a limit in the size of the array. The use of linked lists instead of arrays would solve the deletion problem, but it would create its own problem of rapid access. While each of the above files accesses methods and techniques has its own advantages, there are pitfalls in each case. Therefore all tradeoffs are worth learning and analyzing.