Opening and closing a file

Opening a file: To open a file the following syntax is used:

Syntax:            fopen(“filename”,”mode”);

File modes: There are three types of modes to open a file these are as follows:

r: To open a file for reading purpose. And if the file is exists then the file will open for the reading. Otherwise an error occurs.

w: To open a file for writing purpose. If the file is not exists then the file is created with specified name otherwise it deleted the contents of existing file and starts writing from begins.

a: To open a file for appending. It opens the file without deleted existing contents and if file is not exists then it created file with specified name.

 

FILE: Before use any file we should declare the file of type FILE. Data structure of a file is defined as FILE. And this defined in the library of standard I/O. The fopen() returns the address of the structure and this can be store to a structure pointer.

e.g:      FILE  *fptr;

fptr = fopen(“file-name”,”mode”);

Closing a file: After access a file, it must be closed. There are many reasons like security and to avoid threats for data.

 Syntax:          fclose(file-pointer);

Posted in C

Leave a Reply

Your email address will not be published. Required fields are marked *