Sunday, January 8, 2012

What does FILE *fp means ??

What exactly does the following statement means?
'FILE *fp;
fp=fopen("filename","mode");
'

Many of you might answer:we have declared a file pointer 'fp' which points to the file 'filename'.
But actually,this is not correct.

C has a special "data type" for handling files which is defined in the standard library 'stdio.h '.
It is called the file pointer and has the syntax FILE*.

First of all let me confirm you that **FILE is not a key word** , perhaps its a user defined data type defined in 'stdio.h' , presenting you the following code to justify my answer !

typedef struct {
int level; /* fill/empty level of buffer */
unsigned flags; /* File status flags */
char fd; /* File descriptor */
unsigned char hold; /* Ungetc char if no buffer */
int bsize; /* Buffer size */
unsigned char *buffer; /* Data transfer buffer */
unsigned char *curp; /* Current active pointer */
unsigned istemp; /* Temporary file indicator */
short token; /* Used for validity checking */
}FILE;

This is the structure stored in 'stdio.h' under the name FILE.This file pointer is just to store the composite information about a file subject to manipulation.These components specify certain things about the file to the operating system.
Thus, FILE, which contains all the information about the file, is subsequently used as a communication link between the system and the program.

Starting with the actual process which happens when we use 'FILE':
First of all,u all should know that while doing any operation on a file in C,we are not working on the actual file.
What happens is that the contents of the file we want to perform, are copied in a memory buffer.

and whenever we execute the above two statements, a structure as defined above is created,its elements are setup for the file referred and the base addresss of this srtucture is returned in pointer 'fp'.Thus,fp is not pointing to the file's buffer.
Within the structure to which fp is pointing,there is a character pointer called 'buffer'(refer to the structure).It is this pointer which points to the file's buffer.
It can be explained like this:
address of structure=1500 => address stored in fp=1500
address of file buffer=1700 => address stored in 'char *buffer'=1700

and we dont need to increment this 'buffer' pointer while using func such as getc().This is done by the function itself.To do this internally, operation of the form 'fp->buffer=fp->buffer+1' is done.

This whole information is a basic prerequisite if u want to start-up with using file i/o functions.

hope, this is helpful:)

6 comments:

Sourabh Girdhar said...

Hi this is very informative... nice one..
Could you please tell me how does random access files work and how to work with them in C...

g said...

ya...actually, that will be included in my next post..

Anonymous said...

Thank you so much for clearing this doubt :)

Varun M Ram said...
This comment has been removed by a blog administrator.
Varun M Ram said...

why is FILE declared in uppercase? why not in lowercase?

g said...

FILE is the structure's name, fp is declared as FILE pointer type