Sunday, March 18, 2007

File Handling in C

File Handling in C - File Pointers

C communicates with files using a new datatype called a file pointer.This type is defined within stdio.h, and written as FILE *.

Let us declare a file pointer called newfile,
FILE *newfile;

Opening a file pointer using fopen

The file must be opened before it can be accessed in a program.
This is done using the fopen function, which returns the required file pointer.

If the file cannot be opened for any reason then the value NULL will be returned.

Here is an example showing how to open an file,

if ((newfile = fopen("newfile", "w")) == NULL)
fprintf(stderr, "Cannot open %s\n", "newfile");

In the above example file named newfile that we declared above is opened using fopen in write mode and that file is checked whether it is NULL, If the file is does not exist then a error is displayed Cannot open newfile

fopen takes two arguments, both are strings, the first is the name of the file to be opened, the second is an access character, which is usually one of:
"r" Open the file for reading
"w" Open the file for writing
"a" Open the file for appending

Closing a file using fclose

The fclose command can be used to disconnect a file pointer from a file. This is usually done so that the pointer can be used to access a different file. Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.

The closing statement looks like this,

fclose(output_file);

If files are still open when a program exits, the system will close them for you as the program closes. However it is usually better to close the files properly.
.....................................................................................................................................



Input and Output using file pointers

Having opened a file pointer, you will wish to use it for either input or output.
C supplies a set of functions to allow you to do this. All are very similar to input and output functions that you have already met.

Character Input and Output with Files

This is done using equivalents of getchar and putchar which are called getc and putc. Each takes an extra argument, which identifies the file pointer to be used for input or output.

putchar(c); is equivalent to putc(c,stdout);
getchar(); is equivalent to getc(stdin);

Formatted Input Output with File Pointers

Similarly there are equivalents to the functions printf and scanf which read or write data to files. These are called fprintf and fscanf.

The functions are used in the same way, except that the fprintf and fscanf take the file pointer as an additional first argument.

Whole Line Input and Output using File Pointers

Predictably, equivalents to gets and puts exist called fgets and fputs. The programmer should be careful in using them, since they are incompatible with gets and puts. gets requires the programmer to specify the maximum number of characters to be read. fgets and fputs retain the trailing newline character on the line they read or write, wheras gets and puts discard the newline.

When transferring data from files to standard input / output channels, the simplest way to avoid incompatibility with the newline is to use fgets and fputs for files and standard channels too.

For Example, read a line from the keyboard using
fgets(data_string, 80, stdin);

and write a line to the screen using
fputs(data_string, stdout);
.............................................................................................................................



Special Characters

C makes use of some 'invisible' characters

NULL, The Null Pointer

NULL is a character or pointer value. If a pointer, then the pointer variable does not reference any object (i.e. a pointer to nothing).It is usual for functions which return pointers to return NULL if they failed in some way. NULL is returned by read commands of the gets family when they try to read beyond the end of an input file.

Where it is used as a character, NULL is commonly written as '\0'. It is the string termination character which is automatically appended to any strings in your C program. You usually need not bother about this final \0', since it is handled automatically. However it sometimes makes a useful target to terminate a string search.

EOF, The End of File Marker

EOF is a character which indicates the end of a file. It is returned by read commands of the getc and scanf families when they try to read beyond the end of a file.

No comments: