2 Advantages of files
The concept of a file is flexible. The most basic operations associated with a file include the following:
- Open: associates a memory-based data structure with a file.
- Read: transfers portions of the content of a file into memory (for processing).
- Write: transfers portions of memory content into a file.
- Close: release the association of the memory-based data structure from a file.
Besides the simplicity of accessing files, files also offer some other advantages:
- Identified by symbolic name/path (instead of some numeric value). This makes it possible to organize files in
a way that makes sense to people.
- Accessible by multiple processes at the same time. While simultaneous access to a file by multiple processes
must be carefully done, it is a flexible method for multiple processes to communicate and share resources.
Because files are so useful, Unix-like operating systems, such as Linux, includes many commands to perform many
file-related operations. These commands can be “stringed” together to perform more complex operations.
Furthermore, every program already has three implied files:
- stdin (aka cin in C++): this is the standard input file. Most programs that process one single file use stdin
as the file to be processed. Normally, a program does not need to “open” stdin, as it is already opened by the
shell environment.
- stdout (aka cout in C++): this is the standard output file. Most prorams that produces one single file use
stdout as the output. As with stdin, the shell environment opens a file as stdout before a program assumes
control.
- stderr (aka cerr in C++): this is the standard error file. It is an output file, like stdout. However, stderr
is mostly used to report error. This makes it possible for a program to output to a regular file (hence not the
screen), and yet display error messages to the screen (console).
In a Unix-like environment, processing files is easy, convenient and flexible. But, just what can one do with
files?