C programmers can create, open, close text or binary files for their data storage.

   A file represents a sequence of bytes, does not matter if it is a text file or 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. This chapter will take you through important calls for the file management.

Opening a File

    You can use the fopen( ) function to create a new file or to open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to control the stream. Following is the prototype of this function call:

				FILE *fopen( const char * filename, const char * mode );	
			

Here, filename is string literal, which you will use to name your file and access mode can have one of the following values:

Closing a File
Mode Description
r Opens an existing text file for reading purpose.
w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file.
a Opens a text file for writing in appending mode, if it does not exist then a new file is created.
r+ Opens a text file for reading and writing both.
w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist.
a+ Opens a text file for reading and writing both. It creates the file if it does not exist

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. This function actually, flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h.

Writing a File

Following is the simplest function to write individual characters to a stream:

					int fputc( int c, FILE *fp );
				

The function fputc() writes the character value of the argument c to the output stream referenced by 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 );
				

The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success

    Try the following example:

					#include <stdio.h>
					main()
					{
					FILE *fp;
					fp = fopen("/tmp/test.txt", "w+");
					fprintf(fp, "This is testing for fprintf...\n");
					fputs("This is testing for fputs...\n", fp);
					fclose(fp);
					}
				

When the above code is compiled and executed, it creates a new file test.txt in /tmp directory and writes two lines using two different functions. Let us read this file in next section

Reading a File

Following is the simplest function to read a single character from a file:

					int fgetc( FILE * fp );
				

The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error it returns EOF. The following functions allow you to read a string from a stream:

					char *fgets( char *buf, int n, FILE *fp );
				

The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string. Program:

					#include <stdio.h>
					main()
					{
					FILE *fp;
					char buff[ 100 ];
					fp = fopen("/tmp/test.txt", "r");
					fscanf(fp, "%s", buff);
					printf("1 : %s\n", buff );
					fgets(buff, 255 , (FILE*)fp);
					printf("2: %s\n", buff );
					fgets(buff, 255 , (FILE*)fp);
					printf("3: %s\n", buff );
					fclose(fp);
					}

When the above code is compiled and executed, it reads the file created in previous section and produces the following result:

1 : This
2 : is testing for fprintf...
3: This is testing for fputs...