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 :
|
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:
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