HOME C,C++ HOME



tally erp9 training

PROGRAMMING IN C

FILE HANDLING

FILE HANDLING

INTRODUCTION:

Until, we have been using the functions such as scanf and printf to read and write date. These are console oriented I/O functions, which always use the terminal (keyboard and screen) as the target place. This works fine as long as the data is small. However, many real-life problems involve large volumes of data and in such situations, the console oriented I/O operations pose two major problems.

1. It becomes cumbersome and time consuming to handle large volumes of data through terminals.
2. The entire data is lost when either the program is terminated or the computer is turned off.

It becomes necessary to have a more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data. A file is a place on the disk where a group of related data is stored.

Many application require that information be written to or read from an auxiliary storage. Such information is stored on the device in the form of a “data file”. Thus, data files allow us to store information permanently, and to access and alter that information whenever necessary.

In C, an extensive set of library functions is available for creating and processing data files. Unlike other programming language, ‘C’ does not distinguish between sequential and direct access (random access) data files. There are, however, two different types of data files, called stream-oriented (or standard) data files, and system-oriented (or low-level) data files. Stream-oriented data files are generally easier to work with than system-oriented data files and are therefore more commonly used.

“Stream-oriented data files” can be divided into two categories

1. In the first category are data files comprising consecutive characters. These characters can be interpreted as individual data items or as components of strings or numbers.
2. The second category of stream-oriented data files, often referred to as unformatted data files, organizes data into blocks containing contiguous bytes of information.

“System-oriented data files” are more closely related to the computer’s operating system than are stream-oriented data files. They are somewhat more complicated to work with, though their use may be more efficient for certain kinds of applications. A separate set of procedures, with accompanying library functions, is required to process system-oriented data files.

OPENING AND CLOSING A DATA 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. This buffer area allows information to be read from or written to the data file more rapidly than would otherwise be possible. The buffer area is established by writing

FILE *ptvar;
Where FILE is a special structure type that establishes the buffer area, and ptvar is a pointer variable that indicates the beginning of the buffer area. The structure type FILE is defined within a system “include” file, typically “stdio.h”.
 
       The library function ‘fopen’ is used to open a file. This function is typically written as
             ptvar=fopen(file_name, file_type);
                   file_type is otherwise called as “mode”.

The “fopen” function returns a pointer to the beginning of the buffer area associated with the file. A NULL value is returned if the file cannot be opened as, for example, when an existing data file cannot be found.

Finally, a data file must be “closed” at the end of the program. This can be accomplished with the library function “fclose”. The syntax is simply    br>fclose(ptvar);



FILE-TYPE SPECIFICATION:

File Type Meaning
“r” Open an existing file for reading only.
“w” Open a new file for writing only.  If a file with the specified ‘file-name’ currently exists, it will be destroyed and a new file created in it’s place.
“a” Open an existing file for appending. A new file will be created if the file with the specified ‘file-name’ does not exist.
“r+” Open an existing file for both reading and writing.
“w+” Open a new file for both reading and writing. If a file with the specified ‘file-name’ currently exists, it will be destroyed and a new file created in its place.
“a+” Open an existing file for both reading and appending. A new file will be created if the file with the specified ‘file-name’ does not exist.


/*EX-59: READ A LINE OF LOWER-CASE TEXT AND STORE ITS UPPER-CASE EQUIVALENT WITHIN A DATA FILE*/
#include<stdio.h>
main()
{
FILE *fpt; /* Define a pointer to pre-defined type FILE */
char c;
clrscr();
fpt=fopen("f_ex1.dat","w"); /* Open a new data file for writing only */
do
putc(toupper(c=getchar()),fpt);
while(c!='\n');
fclose(fpt); /* Close the data file */
}}
 
 

The program begins by defining the stream pointer ‘fpt’, indicating the beginning of the data file buffer area.

The loop continues as long as a new line character (\n) is not entered from the keyboard. Once a new line character is detected, the loop ceases and the data file is closed.
 
/* EX-60: FILE CREATION(WRITE) PROGRAM */
#include<stdio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("f_ex2.dat","w");
if(fp==NULL)
{
printf("\n\tERROR: Cannot open the designated file");
exit(1); /* abnormal termination */
}
while(1) /* Infinite loop */
{
ch=getchar();
if(ch==EOF) /* EOF = ^Z */
break;
else
fputc(ch,fp);
}
fclose(fp);
}
 
 

Note: Input will terminate when you press ^Z in data file. We can see the content of file f_wr2.dat from DOS Prompt using type The content of existing file “f_ex2.dat” can read by accessing a separate program, i.e., as follows.
 
/* EX-61: FILE READ PROGRAM */
#include<stdio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen("example.dat","r");
if(fp==NULL)
{
printf("\nFile opening error");
exit(2);
}

while(1)
{
ch = fgetc(fp);
if(ch == EOF)
break;
else
putchar(ch);
}
fclose(fp);
getch();
}
 
 

FILE COPY: Until we have seen the program for write the data into file and read the data from file. By using following program, we can copy the content of source file into the target file in a single program.
 
/* EX-62: FILE COPY *//td>
#include<stdio.h>
main()
{
FILE *fs, *ft;
char ch;
clrscr();
fs=fopen("example.dat","r");
if(fs==NULL)
{
printf("\n File opening error");
exit(0);
}
ft=fopen("temp.dat","w");
if(ft==NULL)
{
printf("\n File creation error");
exit(2);
}

while(1)
{
ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft);
}
printf("\n Files copied");
fclose(fs);
fclose(ft);
getch();
}