i) file. If the file does not exist, then

i) Declaring a file pointer variableTo access a particular file, you must specify the name of the file that has to be used which is accomplished by using file pointer variable that points to the file. The syntax for declaring file pointer variable is,FILE *file_pointer_variableExample, if we write FILE *fpThen fp is declared as a file pointer.ii) Opening a fileTo modify the data in a file, it must be opened first. The function used here is fopen().

The prototype of function is,FILE *fopen(const char *file_name, const char *mode)Here, the file is opened in the mode specified using the ‘mode’. If successful, fopen() returns a file_pointer and if it fails, it returns NULL.File name:Every file on the disk has a name known as the file name. The naming conventions vary from one operating system to another. Windows and Unix permit filenames having maximum of 256 characters.

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!

order now

In C, the fopen() may contain pathname. If a file name is specified without a path, it is assumed that the file is located in the current working directory.File mode:The different modes in which file can be opened are given in the tableMode Descriptionr Opens a text file for reading only. If the file does not exists, then an error will be thrownw Opens a file for writing only. If the file already exists, then the contents of the file will be deleted.If the file does not exist, then it is created.a Appends data to a text file.

If the file does not exist, then it is created.rb b indicates binary. It opens a binary file for reading.wb Opens a binary file for writing.ab Appends to a binary file.r+ Opens a file for both reading and writing. The file must already exist.w+ Opens a file for both reading and writing.

The stream will be created if it does not exist. If it exists,then it is truncated.a+ Opens a file for both reading and writing.iii) Closing a fileTo close a file, fclose() function is used. After, fclose() function has disconnected the file pointer from the file, the file pointer can be used to access different file.

The prototype of fclose() is given as,int fclose(FILE *fp);Here, fp points to the file which is supposed to be closed. You can see that the return type is ‘int’. If the function was successful, it returns zero. If it fails, it returns non-zero value.Example:#includemain(){FILE *fp;fp = fopen(“abc.txt”,”r”);if(fp==NULL){printf(“The file could not be opened”);exit(1);}fclose(fp);return 0;}When this code is compiled, it creates a new file abc.txt if the file does not already exist.