Google Find us on Google+

Sunday, 21 April 2013

Opening And Closing A Data File.

Files are not only used for data, but our programs are also stored in files. To work with a file, a program has to go through the following steps:
  1. Open a file for reading or writing.
  2. Do operation read or write to file using library functions.
  3. Close the file may be used by some other program.
The difference between working with file or with standard input/output terminals is that we must specify the location of file to work within our program.

Opening a File:

Specifying the file you wish to use is referred to as opening the file. When working with a stream oriented data file, the first step is to establish a buffer area,where information is temporarily stored while being transferred between the computer's memory and the data file. The buffer area allows information to be read from or written to the data file more rapidly than would other wise be possible.

Buffered input/output:

A buffer is a region of memory used to temporarily hold data while it is being moved from one place to another. Typically the data is stored in buffer(i.e.memory location) as it is retrieved from an input device (such as a keyboard) or just before it is sent to an output device (such as monitor). 
The buffer area is used using the pointer to FILE declared as:
FILE *ptvar;
where,
FILE (only uppercase letters should be required) is a special structure type that establishes the buffer area, and ptvar is a pointer variable that indicates the beginning of the buffer area(courser). It is often referred to as stream pointer, or simply a stream. The structure type FILE is defined within a system include file, typically stdio.h.

A data file must be opened before it can be created or processed. A file is opened by call to function fopen() which is available in library stdio.h.
The syntax of function fopen() is
ptvar=fopen(file name, file type);
where file name and file type are the stings that represent the name of the data file and the manner in which the data file will be utilised. It means that the file type is just a mode which specifies the purpose of opening a file; reading a file, or writing a file, or appending text at the end of the file(i.e.continuation of writing at the end of the file), etc.
The name chosen for the file must be consistent with the rules for naming files, as determined by the computer's operating system.
The function returns a pointer to the object which controls the stream associated with the file, that is, the pointer to the beginning of buffer area associated with the file(if the file is opened successfully), otherwise it returns a NULL value which is defined in stdio.h.
For example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("cse.txt","r");
}
The above program will open a file "cse.txt" for reading. If no file exists with this name in the current directory, then the function will return NULL value, indicating no valid buffer is associated within the file.
Considering another example:
#include<stdio.h>
void main()
{
FILE *fp;
fp=fopen("cse.txt","w");
}
The above code will open a file named "cse.txt" for writing. If the file does not exists within the given path, then the function will create a new file and opens it to write. If the file already exists, then it will be opened in write mode but the previous contents will be deleted.
Every file has its own pointer variable. When we wish to read from a file or write to a file, we specify the file pointer variables as FILE *fp1, *fp2, *fp3;
The variables fp1,fp2, and fp3 are file pointers.
Example for a NULL pointer:
#include<stdio.h>
#include<conio.h> 
#include<stdlib.h>   //prototype for exit function
void main()
{
FILE *fp;
fp=fopen("cse.txt","w");
if(fp==NULL)
{
printf("cannot open the file");
exit(0);  //terminates the program.
}
 //here we can work on file(i.e. write to a file)..
}
If fopen() returns NULL, we can check whether the file is opened or not and accordingly print the message in to the file.
We can also combine the fopen() and if statements as follows:
if((fp=fopen("cse.txt","w"))==NULL)
printf("cannot open the file");
exit(0); //terminates the program.

Closing a File:

When all read, write, and other operations are done with an opened file, it must be closed immediately for the following reasons:
  • To reopen it in some other mode.
  • To prevent any misuse with the file.
  • To make the operations you have performed on the files successful.
To close a file, we must use the function named fclose().
The syntax of fclose() is as follows:
fclose(ptvar)
where ptvar is a pointer variable(fp).
The function fclose() closes the named stream. Pointer variable points to a file to be closed. All buffer associated with the stream are flushed before closing the file. On success, fclose() returns 0 otherwise, it returns end-of-function.
Example program:
A program code which demonstrates the functions fopen() and fclose().


#include<stdio.h>
#include<conio.h> 
#include<stdlib.h>   //prototype for exit function.
void main()
{
FILE *fp;
clrscr();
fp=fopen("cse.txt","w");
if(fp==NULL)
{
printf("cannot open the file");
exit(0); //terminates the program.
else
{
printf("File is opened successfully");
printf("\n Now you can work on your file in write mode");
}
if(fclose(fp)==0)
{
printf("\n File is closed now. cannot work on file");
}
getch();
}

Output for the above given program:
File is opened successfully
Now you can work on your file in write mode
File is closed now. cannot work on file

How the above program works?
In the above code, a file named "cse.txt" is opened. The if condition checks whether file is opened or not. If the file is not opened, it prints the message "cannot open the file" and terminates the program. If the file is opened, it prints the corresponding message. The condition fclose(fp)==0 checks whether the file pointed to by fp is closed successfully or not.
  •  Note: A file pointer is simply a variable like an integer or a character. It does not point to a file or the data in a file; it is simply used to indicate which file refers to the input/output operations.
From site : http://ctechrockz.blogspot.com/

No comments:

Post a Comment