facebook like button

03 June, 2011

FILE HANDLING in C (pointers to files)

The need: 
      You people have made a lot of programs till now. You have taken inputs, printed outputs through your programs. Did you ever think what happens to your inputs and outputs when the program terminates and you close the terminal (program window). All those variables, values are lost somewhere in the memory. Now suppose you have to make an inventory program, a catalog or something else which required permanent storage, how you can do that ? The idea is simple we can store the values somewhere else where they can be found later even after the program is terminated. Files are such locations. We have methods by which we can write necessary data into a file and can read back from file. This is a whole branch of C called FILE HANDLING in C. 
   Some points about file handling:
1.  We use a pointer variable to point to a file. This variable is of type FILE and is technically called "file pointer". Ex.
FILE *fp;     //declaration of a file pointer fp
2.  Common sense says that to work with files we need to do some operations which are following: read, write, append, create and delete.
3.  For read, write or append operation to a file first we have to open that file. This is done in the following manner.
fp=fopen(filename,"r");     //using fp to open a file
Here fopen is a built-in function which takes 2 arguments. This function opens a file whose name is stored in a string variable filename and returns a pointer value to fp. The name of file can also be directly given but in that case name should be enclosed in double quotes. The second argument to this function is mode of opening the file. Here r in double quotes implies that the file is opened only for reading.
We have three modes:
    a. read --- "r"
    b. write --- "w"
    c. append --- "a" 
We can also open a file in more than one mode.
4.  When program reads a file it may or may not read sequentially depending on the need. If it does not read sequentially, it will require jumps (file pointer has to seek through file for that). We have built-in function fseek for this purpose . The syntax is:
fseek(fp,2,1);    //to jump 2 characters ahead in file from the current position
This fseek function takes 3 arguments. First is file pointer of concerned file, second is number of characters to jump (+ve for going ahead -ve for going back) and third is the reference position( 0 for start, 1 for current, 2 for end).
5.  For all these built-in functions header file <stdlib.h> should be included.
6. The most important point is introduction of the character EOF. As the character NULL signifies the end of a string, the character EOF signifies the end of the file. This non-printable character is by default added at the end each file.
    More points will be added as more programs are added in upcoming posts.

Remarks:


You may skip remark below for now if it seems confusing to you.
    One question can arise here which is: "where does file pointer point?"
Answer to this question you may not be able to find on internet or may be you can find. But my observation says that file pointer points to some location in the memory and that memory location holds the record of current position of cursor(actually I should say imaginary cursor because there is no cursor visible). This can be verified with following code...

----------------------------------------------
#include<stdio.h>
#include<stdlib.h>
main()
{
    FILE *fp;
    char c;
    fp=fopen("check.txt","r");
    printf("fp is %d\t *fp is %d\n",fp,*fp);
    c=getc(fp);
    printf("fp is %d\t *fp is %d\n",fp,*fp);
    c=getc(fp);
    printf("fp is %d\t *fp is %d\n",fp,*fp);
    c=getc(fp);
    printf("fp is %d\t *fp is %d\n",fp,*fp);
    c=getc(fp);
    printf("fp is %d\t *fp is %d\n",fp,*fp);
}

----------------------------------------------

when you run the above code make sure that you have a test file check.txt in the same folder in which above code resides.

No comments:

Post a Comment

feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply