-->

File Management

Definition of File :  File is a collection of bytes that is stored on secondary storage devices like disk.

File pointer variable : It is similar to pointer variable  , but it hold the address of the file  to perform operation on file like read,  write append   etc . 

Syntax : FILE *file_pointer

 where FILE is keyword data type  used declare file  variable.
file pointer keep the record of access.

File Opening & closing function in C   
The sequence of any kind of operation on data file is done by performing 
opening file 
performing read & write operation on file 
closing file 

fopen( ) function :
 This function is used open a file for different operation 
Syntax 
             fopen( file_name_with extension , Mode_of_operation);
  
        where file name : Address of file where located in system with extension 
                   mode : 

r
 file is open for  read only. No change cannot be made 
w
Create a new file and write to it. If a file with the same name already exists, overwrite it – rather, erase the existing file and create a new one.
a
Append to the end of a file. If the file does not exist, create a new one and write to it.
r+
Open an existing file. Allow both read and write operations to this file. Note – file must exist a priori.
w+
Create an empty file. Allow both reading and writing.
a+
Open an existing file. Allow both reading and writing.


if file name is correct ,file access is   successful, returns a pointer to a FILE object 
   else fails, returns NULL

Example 1:   fopen( "studname.txt","r") 
                    file studname is opened for read only 
 Ex 2: 
                    FILE * fp;
           fp = fopen ("studname.txt", "w");
          file  studname.txt  is opened for write only 

 How to check wether file is exit or not 
       FILE *fp = fopen(“myfile.txt”, “r”); 
     if (fp == NULL)
       { 
         //report error and try to recover 
           }
     else
     {
    //do something with the file 
      }

       fclose( ) function
      This function is used to close the file which are opened by fopen( ) function
       syntax
                  fclose(file pointer);
          Example 
          
                FILE *fp;
                fp= fopen("Stud.txt", "r");
                fclose(fp);

1 comment:

Kani Mozhi said...

I have read many article here and learn many things from that, this was really helpful for me. Thank you so much for sharing this info with us and share your ideas with us.
Document Management Software
Electronic Document Management System
Document Management System
Cloud Document Management Software

Post a Comment

Basic structure of c program