07-11-2012, 05:29 PM
File Handling in C
FilesinC lecture.doc (Size: 77 KB / Downloads: 120)
We frequently use files for storing information which can be processed by our programs. In order to store information permanently and retrieve it we need to use files.
Files are not only used for data. Our programs are also stored in files.
The editor which you use to enter your program and save it, simply manipulates files for you.
The Unix commands cat, cp, cmp are all programs which process your files.
In order to use files we have to learn about File I/O i.e. how to write information to a file and how to read information from a file.
We will see that file I/O is almost identical to the terminal I/O that we have being using so far.
The primary difference between manipulating files and doing terminal I/O is that we must specify in our programs which files we wish to use.
As you know, you can have many files on your disk. If you wish to use a file in your programs, then you must specify which file or files you wish to use.
Specifying the file you wish to use is referred to as opening the file.
When you open a file you must also specify what you wish to do with it i.e. Read from the file, Write to the file, or both.
Because you may use a number of different files in your program, you must specify when reading or writing which file you wish to use. This is accomplished by using a variable called a file pointer.
Every file you open has its own file pointer variable. When you wish to write to a file you specify the file by using its file pointer variable.