- A file is a place on disk, where group of related data is stored.
- Two different types of data files exist in C
- Stream-oriented(standard) data files.
- System-oriented(Low-level) data files.
- Stream oriented data files are of two types
- Data files comprising of consecutive charecters.
- Unformatted data files.
- System-oriented data files are more closely related to the computers operating system.
- System oriented files are complicated to work with, their usage is comparitively limited to certain particular applications.
Basic file operations in C
- Naming a file
- Opening a file
- reading data from a file
- writing data to a file
- closing a file.
- Before using a file, a buffer area has to be established for that file.The information is normally stored in 'buffer area' while its transfer between computer memory and datafile is being taken.
- The statement for establishing such an area is FILE *ptrvar;
- ptrvar is pointer variable name.
- FILE is a defined data type.
Opening a file
|File opening Mode
||open an existing file read only
||open a new file for write only
||open an existing file appending
||open an existing file for both reading and writing
||open a new file for both reading and writing
||open an existing file for both reading and appending
- fopen opens the file 'file.txt' and associates it with the pointer ptr.
- "w" stands for the mode 'write-only'.
closing a datafile
- fclose(ptrvar); whrere ptrvar is pointer variable name.
union number val;