A file represents a sequence of bytes on the disk where a group of related data is stored. File is created for permanent storage of data.
In C language, we use a structure pointer of file type to declare a file.
FILE *fp;
C provides two different ways of file processing. These are
Standard Input/output (stream I/O or high-level I/O).
System-oriented Input/output (low-level I/O).
The Standard I/O is very simple and most commonly used way of performing file I/O in C language.Here data is written as individual characters or as strings.
The System-oriented I/O is complex to implement. The data cannot be written as individual characters, or as strings, or as formatted data. Using this approach the data must be written as a buffer full of bytes, where user needs to create this buffers manually.
C provides a number of functions that helps to perform basic file operations. Following are the functions
fopen(): This command is used to open a particular file.
General Syntax :
*fp = FILE *fopen(const char *filename, const char *mode);
Example
fp = fopen("one.txt", "w");
Here filename is "one.txt" and mode w specifies the write mode.
fclose(fptr): This command is used to close a file.
General Syntax :
int fclose( FILE *fp );
Here fclose() function closes the file and returns zero on success, or EOF if there is an error in closing the file. This EOF is a constant defined in the header file stdio.h
getc (): Used to read from the file.(SingleCharacter)
char input = getc(fp); where fp is file pointer.
putc(): Used to write into the file. (SingleCharacter)
putc('c',fp); where character 'c' is written in file pointed by file pointer fp;