C File Management

Defining and opening a file

		FILE *fp; 
		fp=fopen(“filename”,”mode”); 
The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library.
The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program.
The second statement also specifies the purpose of opening the file. The mode does this job. R open the file for read only. W open the file for writing only.
A open the file for appending data to it.

Closing a file

#include< stdio.h > 
main() 
{  file *f1; 
   printf(“Data input output”); 
   f1=fopen(“Input”,”w”); /*Open the file Input*/ 
   while((c=getchar())!=EOF) /*get a character from key board*/ 
   putc(c,f1); /*write a character to input*/ 
   fclose(f1); /*close the file input*/ 
   printf(“\nData output\n”); 
   f1=fopen(“INPUT”,”r”); /*Reopen the file input*/ 
   while((c=getc(f1))!=EOF) 
   printf(“%c”,c); 
   fclose(f1); 
 } 
        
The data enter through the keyboard and the program writes it. Character by character, to the file input. The end of the data is indicated by entering an EOF character, which i
fclose(file_pointer);
A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.

 

Make Comments..!!


Oops!! No posts from user.

Download Android App