This video cover how C programmers can create, open, close text or binary files for their data storage.A file represents a sequence of bytes, regardless of it being a text file or a binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.
Opening Files
You can use the fopen( ) function to create a new file or to open an existing file.
Closing a File
To close a file, use the fclose( ) function. The prototype of this function is
int fclose( FILE *fp );
The fclose(-) function returns zero on success, or EOF if there is an error in closing the file.
Writing a File
Following is the simplest function to write individual characters to a stream
int fputc( int c, FILE *fp );
It returns the written character written on success otherwise EOF if there is an error. You can use the following functions to write a null-terminated string to a stream :
int fputs( const char *s, FILE *fp );
It returns a non-negative value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format, ...) function as well to write a string into a file.
Reading a File
Given below is the simplest function to read a single character from a file
int fgetc( FILE * fp );
The return value is the character read, or in case of any error, it returns EOF.
The following function allows to read a string from a stream
char *fgets( char *buf, int n, FILE *fp );