- In my previous post titled “What is File?” I explained about the concept of file, file attributes and file types. This post is the supplementary for the same so if you haven’t read that post yet, I strongly recommend to read that first before proceeding to this one. This post deals with the basic operations performed on the file.
- A file is an abstract data type. To define a file properly, we need to consider the operation performed on the file.
- Operating System can provide system calls to create, write, read, reposition, delete and truncate files.
Creating a File
- Creation of a file is the first operation. For creating a file two steps are necessary.
- Required space must be allocated.
- An entry for new file must be made in the directory.
- The directory entry records the name of the file and the location in the file system.
Writing a File
- For file writing operation, we make a system call specifying both the name of a file and the information to be written into the file.
- System searches the entire directory structure to find the location of the specified file.
- System keeps a write pointer that keeps track of writing at the location in the file.
- The system updates write pointer each time whenever a file write operation occurs.
Reading a File
- To perform file read operation, we use a system call that specifies name of the file and the block of the file to read from it.
- Again the directory is searched for the specified file.
- System keeps a read pointer that keeps track of reading location in the file.
- The system updates read pointer each time whenever a file read operation occurs
Repositioning Within a File
- Repositioning within a file operation does not involve any actual input output.
- The directory is searched for the appropriate entry and the current file position is set to a given value.
- It is also known as files seek operation.
Deleting a File
- File deletion operation also requires searching of a specified file entry within the directory structure.
- As soon as the file is deleted, space allocated to that file becomes available for further use.
Truncating a File
- In some cases the user may want to erase the file contents but keep its attributes as it is. This operation is called truncating a file.
- Instead of delete a file and recreating it with same attributes, this function allows all attributes to remain unchanged except the file content.
- File length attribute is reset to a length zero and its file space is released.