A file is a data object whose lifetime may
be greater than the lifetime of a program responsible for creating it, because
it is created on secondary storage devices. It is used to store persistent data
values and information. The files are used mainly for input and output of data
to an external operating environment. The components of the file are called as
records (this term has nothing to do with record data structure).
Types of Files
A file
may be a sequential file, a direct-access file, or an indexed sequential
file. A sequential file can be thought of as a linear sequence of
components of the same type with no fixed maximum bound. The major operations
on the sequential files are:
Open
operation: When a
file is to be used, it is first required to be opened. The open operation
requires two operands: the name of the file and the access mode telling whether
the file is to be opened for reading or writing. If the access mode is
"read," then the file must exist. If the access mode is "write,"
then if the file already exists, that file is emptied and the file position
pointer is set to the start of the file. If the file does not exist then the
operating system is requested to create a new empty file with a given name. The
open operation requests the information about the locations and other
properties of the file from the operating system. The operating system
allocates the storage for this information and for buffers, and sets the
file-position pointer to the first component of the file. The runtime library of
C provides an fopen(name,mode) function for it. This function
returns a pointer to the internal structure called FILE (you get the definition of this structure in stdio.h). This pointer is called a file descriptor;
it is used by the C program to refer to the file for reading or writing
purposes.
Read
operation: This
operation transfers the current file component to the designated program
variable. The runtime library of C provides a function fgetc(fp), where fp is a
file descriptor, for fscanf().
fscanf() is
similar to scanf() except that one extra parameter,
fp, is required to be passed as the first parameter.
The second and third parameters are the same as the first and second parameters
of scanf().
Write
operation: This
operation transfers the contents of the designated program variable to the new
component created at the current position. The runtime library of C provides a
function fputc(c,fp), where fp is a file descriptor, and c is a
character to be written in the file fprintf(). fprintf() is similar to printf() except that one extra parameter,
fp, is required to be passed as the first parameter.
The second and third parameters are the same as the first and second parameters
of printf().
Close
operation: This
operation notifies the operating system that the file can be detached from the
program and that it can deallocate the internal storage used for the file. The
file generally gets closed implicitly when the program terminates without
explicit action by the programmer. But when the access mode is required to be
changed it is required to be closed explicitly and reopened in the new mode.
The runtime library of C provides an fclose(fp) function
for it.
Random Access
Each read
and write operation takes place at a position in the file right after the
previous one. But it is possible that you may need to read or write the file in
any arbitrary order. The runtime library of C provides an fseek(fp, offset, from_where) function for this. This function
forces the current position in the file, whose descriptor is fd, to move by offset bytes from either the beginning of the file, the
current file pointer position, or from the end of the file, depending upon the
value of from_where. The parameter from_where must have one of the values (0, 1, or 2 ) that
represent three symbolic constants (defined in stdio.h) as shown in Table 17.1.
CONSTANT
|
WHERE
|
FILE
LOCATION
|
SEEK_SET
|
0
|
File
beginning
|
SEEK_CUR
|
1
|
Current
file pointer position
|
SEEK_END
|
2
|
End-of-file
|
Program
This
program is designed to handle data such at rollno, name and marks of a
student. In this program, the following operations are performed:
- Information on a new student
is entered and stored in the student.txt file.
- The student.txt file is printed on screen
on the operator's request.
- The student.txt file is sorted on the basis
of marks and stored in the file marks.txt.
- Information on a student
whose rollno is given is printed on
screen.
- The average marks of all
students are calculated.
#include<stdio.h>
int
bubble(int*,int);
void
filewrite();
void
avgmarks();
void
fileprint();
void
filesort();
void
rollin();
/******************** SORTING FUNCTION
************************/
int bubble(int x[],int n)
{
int
hold,j,pass,i,switched = 1;
for(pass
= 0; pass < n-1 && switched == 1;pass++)
{
switched=0;
for
(j=0;j<n-pass-1;j++)
}
switched=1;
hold = x[j];
x[j] = x[j+1];
x[j+1]=hold;
}
}
return(0);
}
/**************** FILE WRITING FUNCTION
***********************/
void filewrite()
{
int
roll,ch,mark;
char
nam[50];
FILE *fp;
clrscr();
fp =
fopen("student.txt","a");
printf("ENTER ROLL NUMBER, NAME, MARKS \n");
ch =1;
while(ch)
{
scanf("%d%s%d",&roll,&nam,&mark);
fprintf(fp,"%d %s %d\n",roll,nam,mark);
printf("\n\n press 1 to continue,0 to stop");
scanf("%d",&ch);
}
fclose(fp) ;
}
/******************** OUTPUTTING DATA ON
SCREEN***************/
void fileprint()
{
int
marks[100],rollno[100],x[100],i;
char
name[100][50];
FILE *fp;
clrscr();
fp =
fopen("student.txt","r");
i=0;
printf("ROLLNO
NAME MARK\n");
while(!feof(fp))
fscanf(fp,"%d %s
%d\n",&rollno[i],&name[i],&marks[i]);
printf(" %d %s
%d\n",rollno[i],name[i],marks[i]);
i=i+1;
}
fclose(fp);
printf("\n\n\nPRESS ANY KEY");
getch();
}
/******************* SORTING FILE
************************/
void filesort()
{
int
marks[100],rollno[100],x[100],n,i,j;
char
name[100][50];
FILE
*fp,*fm;
fp =
fopen("student.txt","r");
fm =
fopen("marks.txt","w");
i=0;
while(!
feof(fp))
{
fscanf(fp,"%d %s
%d\n",&rollno[i],&name[i],&marks[i]);
x[i]=
marks[i];
i=i+1;
}
n=i;
bubble(x,n);
for(i=0;i<n;i++)
{
printf(" %d\t",x[i]);
}
for(i=0;i<n;i++)
{
for
(j=0;j<n;j++)
{
{
fprintf(fm,"%d %s
%d\n",rollno[j],name[j],marks[j]);
}
}
}
fclose(fm);
fclose(fp);
printf("\n\n\nPRESS ANY KEY");
getch();
}
/******************** DATA USING
ROLLNO************************/
void rollin()
{
int
i,roll,ch,mark,roll1;
char
nam[50];
FILE *fm;
ch=1;
while(ch)
{
clrscr();
fm =
fopen("marks.txt","r");
printf(" \n ENTER ROLL NUMBER - ");
scanf("%d",&roll1);
i=0;
while(! feof(fm))
{
fscanf(fm,"%d %s %d\n",&roll,&nam,&mark);
if(roll1==roll)
{
printf("\nROLLNO. NAME MARKS\n ");
printf(" %d %s
%d\n" ,roll ,nam,mark);
break;
}
else
i=i+1;
}
"\n\npress 1 to see student info, 0 to return to main
menu\n");
scanf("%d",&ch);
fclose(fm);
}
}
void avgmarks()
{
int
marks[100],rollno[100],n,i;
float
avg,x;
char
name[100][50];
FILE *fm;
fm =
fopen("marks.txt","r");
i=0;
while(!
feof(fm))
{
fscanf(fm,"%d %s
%d\n",&rollno[i],&name[i],&marks[i]);
x = x
+ marks[i];
i=i+1;
}
n = i;
avg = x/n;
printf("AVERAGE MARKS OF %d STUDENTS ARE - %f ",n,avg);
fclose(fm);
printf("\n\n\nPRESS ANY KEY");
getch();
}
/**************** FUNC.
ENDS************************/
void main()
{
int
marks[100],rollno[100],x[100],n,i,j,roll,c,mark,roll1;
char
name[100][10],nam[50];
while(c!=6)
{
clrscr();
printf("GIVE CHOICE--\n");
printf(" 1 TO ENTER STUDENT
INFO.\n");
printf(" 3 TO SORT FILE ON BASIS OF MARKS\n");
printf(" 4 TO PRINT STUDENT
INFO. USING ROLL NO\n");
printf(" 5 TO FIND AVERAGE
OF MARKS\n");
printf(" 6 TO EXIT\n\n--");
scanf("%d",&c);
clrscr();
switch(c)
{
case
1:
filewrite();
break;
case
2:
fileprint();
break;
case
3:
filesort();
break;
case
4: rollin();
break;
case
5: avgmarks();
break;
case
6:
break;
default:
break;
}
}
}
Explanation
- This program uses the
following functions for its specified operation.
int
bubble(int*,int) —
|
This
bubble sorting technique is used for file sorting.
|
void
filewrite() —
|
Used to
write data of a new student in "student.txt" file
|
void
fileprint() —
|
Used to
print information on students.
|
void
filesort() —
|
|
void
rollin() —
|
Used to
find information on a student using his roll number.
|
void
avgmarks() —
|
Used to
find average marks of all students.
|
- The filewrite() function opens the student.txt file in the append mode,
and data entered is written in the same file. In the void fileprint() file, student.txt is opened in read mode and
data is read from it. This data is printed on the screen.
- filesort() opens the student.txt file in read mode and the
file marks.txt in write mode. The data of
all students is temporarily stored in one buffer consisting of three
arrays: one for rollno, the second for name and the third for marks. At the same time, marks
are stored in the x[ ] array for sorting purposes.
The sorting is done by bubble sort. The result of sorting is available in x[ ]. At this stage, each x[ i ] is compared with marks [ j ] . If a match is found, the
data on that student is stored in the marks.txt file. This process is done for all marks.
- In this way, we get a marks.txt file that is sorted on the
basis of marks. In the void rollin(), the
file marks.txt is used to find the student
whose roll number (rollno) is given. For every line in the file, the
rollno in that file is compared with the rollno to be found. If a match
exists in the file, the data on that student is printed on the screen. The
avgmarks() function uses the file marks.txt. Marks of students are
added to variable X, each time the file pointer
is incremented. Then, the average marks (sre) is displayed on the screen.
- In main function, the switch statement is used to invoke
the function related to the option given by the user.
- In this program, input
consists of the rollno, marks, and name of each student.
- Output depends on the user's
choice. When information on students is to be printed, the program prints
the content of the student.txt file on the screen. When information is
sought on the basis of the roll number, the program prints the rollno,
marks, and name of each student. When the average of marks is found, it
prints that.