i2tutorials

Opening and Closing Files

Opening and Closing Files

 

Opening files: fopen( )

 

The general format of the function used for opening a file is as follows:

 

Syntax:

 

FILE *filepointer;
filepointer=fopen(“filename.filetype”,”mode”);

 

Example:

 

FILE *fp;
fp= fopen(“gitam.txt”,”w”);
where gitam.txt is file name.
where ‘w’ is write mode.

 

Note: The function fopen( ) is compulsory for the files to do any operations, without fopen( ) we cannot perform operations on files.

 

Opening and Closing Files

 

Where will be file saved in the computer?

 

1. A) If you didn’t mention the path, by default the file will be saved in the TC folder meant in the drive where C-Lang is installed.

 

Example:

 

fp=fopen(“hello.txt”, “w”);

 

1. B) If you mention the path in fopen( ) function, then the new file will be saved in that particular path.Path should be mentioned as follows:

 

Example:

 

fp=fopen(“d:\\gitam\\hello.txt”, “w”);
 (Or)
fp=fopen(“d:/gitam/hello.txt”, “w”);

 

Closing the File: fclose( fp)

 

 

Syntax:

 

fclose(filepointer);

 

Example:

 

fclose(fp); // where fp is a file pointer.

 

Exit mobile version