19-06-2013, 11:09 AM
File handling in C
File handling.pdf (Size: 35.46 KB / Downloads: 17)
Today, we will discuss about files which are very important for storing information permanently. We store information in files for many purpose, like data processing by our programs.
What is file ?
A file is a collection of bytes stored on a secondary storage device, which is generally hard disk. The collecition of bytes may be interpreted, for example, as characters, words, lines, paragraphs. Some special functions have been designed for handling file operations. We will discuss it one by one here.
Opening file :
Before we perform any operation on a file, we need to identify the file to the system and open it. We do this by using a file pointer and use the function fopen() for opening a file. Once this is done we can read or write to the file using the fread() or fwrite() functions, respectively. The fclose() function is used to explicitly close any opened file.
Closing File :
The fclose() function is used to close the open file. If the program terminates, it automatically closes all opend files. But it is a good programming habit to close any file once it is no longer to used. This is helpful in better utillzation of the system resources and is very useful when you are working on numerous files simultaneously. Some operating systems place a limit on the number of files that can be open at any given point in time.