click on this link......

LinkGrand.com

Wednesday 13 March 2013

File handling in C

File is used to read or write the content on a disk. To write the information to a file on a disk or read, it must open the file, opening the file establishes a link between program and the operating system.
File is a structure which is defined in the header file stdio.h,each file which we oven have its own file structure.
File structure contains the information about its size, location in the memory. to open the file, we first create a pointer type variable of file structure. fopen method (function) opens a file and it contains 2 parameters
1) path of the file.
2) mode of the file.

There are different modes of file :
1) "r" : It reads the content of the file. If file exist, it loads the file into memory and set the pointer to the firsT character in it. If file does not exist it returns null.

2) "w" : It is used for writing the content in a file. If the file exist, its contents are overwritten and if the file does not exist, a new file is created and if the file is not able to open, it returns a null.

3) "a" : Appending the new content at the end of the file and set the pointer to the first char in it. If the file does not exist, a new file is created and return null if it is unable to open.

4) "w+" : Writing the new content reading them back and modifying the existing content of the file. If file exist, its content are destroyed and if file does not exist, a new file is created and it returns null if it is unable to open a file.

Fclose function :
When you finish reading from the file, we need to close it. fclose function used to close the file.

getc function:
It reads the character from the current pointer position.

puts function:
put the character in at that given location of the pointer.
 

No comments:

Post a Comment