File Operations


In programming it is possible to perform input/output streams of data for text and binary formats. To successfully carry out the input/output operation pointers of type FILE will be needed. FILE structure is located in the “stdio.h” standard library and is used for file management.

Example 1:
A general attempt to open read only file.
FILE* fopen(const char *file_name, const char* mode);
Example 2:
An attempt to open read only readme.txt file.
FILE* input_file= fopen(“readme.txt”, “r”);
After execution of the file and extraction of data, file needs to be removed from the data stream by the function fclose(* input_file).

File reading and writing functions:
1. int fgetc(FILE * stream) - function obtains the next input character from the data stream
2. int fputc(int c, FILE* stream) - function writes the character c to the output stream
3. char * fgets(char *s, int n, FILE *stream) - function obtains the line s from the data stream.
4. int fputs(const char*s, FILE* stream) - function writes the line s to the output stream