File and its Usage
Why need to use files?
Often it is not enough to just display the data on the screen. This is because if the data is large, only a limited amount of it can be stored in memory and only a limited amount of it can be displayed on the screen. It would be inappropriate to store data this data in memory. Memory is volatile and its contents would be lost once the program is terminated. So if need the same data again it would have to be either entered through the keyboard again or would have to be regenerated pro-grammatically. Obviously both these operations would be tedious. At such times it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a file on the disk.
How data is organized on the disk?
All data stored on the disk is in binary form. How this binary data is stored on the disk varies from on OS to another. However, this does not affect the C programmer, since the programmer has to use only the library functions written for the particular OS to be able to perform input/output. It is the compiler vendor’s responsibility to correctly implement these library functions by taking the help of OS.
|-----------------------------| |-----------------------------| |--------------------| |--------------------|
| Our Program |------------- ->| C Library Functions|-------------> | OS | -------->| Disk |
|-----------------------------| |---------------------------- | |--------------------| |--------------------|
- A file is a place on the disk.
- A disk is a circular piece to the coating on an audio tape where a group of a related data is chargeable substance, similar to the coating on an audio tape where a group of a related data is stored permanently.
- It is easier to access a file then reading data from a keyboard.
- File handling in C is very simple, since it essentially treats a file as just a stream of characters and allows input and output in a file.
- Structure of data in variables, arrays and structures are temporary; all such data are lost when the execution of the program ends. Many applications require that information to be read from or written on an auxiliary memory device. Such information is stored in the memory device in the form of a file.
- Files are used for permanent storage of large amount of data.
- A file can be used to write or read data from a disk.
Why need to use files?
Often it is not enough to just display the data on the screen. This is because if the data is large, only a limited amount of it can be stored in memory and only a limited amount of it can be displayed on the screen. It would be inappropriate to store data this data in memory. Memory is volatile and its contents would be lost once the program is terminated. So if need the same data again it would have to be either entered through the keyboard again or would have to be regenerated pro-grammatically. Obviously both these operations would be tedious. At such times it becomes necessary to store the data in a manner that can be later retrieved and displayed either in part or in whole. This medium is usually a file on the disk.
How data is organized on the disk?
All data stored on the disk is in binary form. How this binary data is stored on the disk varies from on OS to another. However, this does not affect the C programmer, since the programmer has to use only the library functions written for the particular OS to be able to perform input/output. It is the compiler vendor’s responsibility to correctly implement these library functions by taking the help of OS.
|-----------------------------| |-----------------------------| |--------------------| |--------------------|
| Our Program |------------- ->| C Library Functions|-------------> | OS | -------->| Disk |
|-----------------------------| |---------------------------- | |--------------------| |--------------------|