C Programming - File Handling

C Programming - File Handling

Concept of Data File
We have used scanf ( ) and printf ( ) for input and output. But in those programs, the
data provided by the user is stored temporarily in the main memory during the program
execution. When we terminate the program or turn off the computer entire data will be
lost. Hence, to store the data permanently we need data file.
Data File: The collection of related data which is stored permanently on the secondary
storage device like hard disk is called data file.
File Handling: The process of doing operations with files such as opening/closing,
reading/writing, deleting /moving/renaming etc is called File Handling.
Sequential and Random file
There are two types of files on the basis of access- the sequential and the random access
file. The sequential access files allow reading the data from the file in one after another.
The random access files allow reading data from any location in the file. Sometimes, we
need to read data file from reverse, middle and from specific location.
Steps of creating a file:
1. Create a pointer variable of FILE data-type
Syntax: - FILE file pointer;
Eg: - FILE *fp;
[FILE must be in Capital letter]
2. The file must be opened before using it.
Syntax:
File-pointer = fopen (“file_name” , ”mode” );
Example:
fp = fopen (“employee.dat”,”w”);
“MODE” can be any one of the following.
Mode Description
r open file for reading or display
w open file for writing
a open file for appending
The similarity between ‘w’ and ‘a’ mode is that both are used for writing in a file. The
difference between ‘w’ and ‘a’ mode is that-previous contents are erased and new
contents are added when file is opened in ‘w’ mode, where as previous contents are
preserved and new contents are also added at the end of file in ‘a’ mode.
fprintf ( ) and fscanf( ) functions
Function fprintf( ) is used to write formatted data on the data file where as the function
fscanf( ) is used to read formatted data from the data file.
Syntax:
fprintf (file-pointer , ”format specifier lists” , variable lists);
fscanf (file-pointer , ”format specifier lists” , variable lists);
fread ( ) and fwrite ( ) functions
Function fread( ) is used to read structure variable i.e. record from the data file where as
fwrite ( ) is used to write structure variable to the data file.
Syntax:
fread ( & variable, sizeof(structure_name),1,fp);
fwrite ( & variable, sizeof(structure_name),1,fp);
where,
variable is structure type variable of structure_name ,
sizeof( ) functions determines the memory size of structure,
1 specifies the number of record can be accessed at once,
fp is file pointer.
fclose () function:
It is used to close the opened file.
Syntax:
fclose(file_pointer);
Example:
fclose(fp);
65. WAP which asks name and age of a person and store it in a file “record.dat”?
#include<stdio.h>
#include<conio.h>
main( )
{
int age;
char name[20];
FILE *fp;
fp=fopen("record.dat","w");
printf("Enter name=");
scanf("%s",name);
printf("Enter age=");
scanf("%d",&age);
fprintf(fp,"%s\n%d",name,age);
fclose(fp);
getch( );
}
66. WAP to display contents from the file ”record.dat” ?[HSEB2064]
#include<stdio.h>
#include<conio.h>
main( )
{
int age;
char name[20];
FILE *fp;
fp=fopen("record.dat","r");
fscanf(fp,"%s%d",name,&age);
printf("Your name = %s\n",name);
printf("Your age = %d",age);
fclose(fp);
getch( );
}
67. WAP to delete and rename the data file using remove and rename command?
#include<stdio.h>
#include<conio.h>
main( )
{
char name[20];
int age;
FILE *fp;
fp=fopen("record.dat","w");
printf("Enter name=");
scanf("%s",name);
printf("Enter age=");
scanf("%d",&age);
fprintf(fp,"%s\n%d",name,age);
fclose(fp);
rename("record.dat","rec.txt");
remove("rec.txt");
getch( );
}
68. Write which asks name, age and gender of student and write it in a file
“student.dat”.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20],gen[20];
int age;
}s;
main( )
{
FILE *fp;
fp=fopen("student.dat","w");
printf("Enter name,age and gender of student=");
scanf("%s%d%s",s.name,&s.age,s.gen);
fprintf(fp, "%s%d%s",s.name,s.age,s.gen);
fclose(fp);
getch( );
}
69. Write a program which reads name, department and age from a file named
“employee.dat” and display them on the monitor? [HSEB-2069,70-D,72-E]
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[20],dept[20];
int age;
}e;
main( )
{
FILE *fp;
fp=fopen("rec.txt","r");
while((fscanf(fp,"%s%s%d",e.name,e.dept,&e.age))!=EOF)
{
printf("\n%s\t%s\t%d\n",e.name,e.dept,e.age);
}
fclose(fp);
getch( );
}
70. WAP which writes “Welcome to Nepal” in a file.[HSEB-2070-C]
#include<stdio.h>
#include<conio.h>
main( )
{
FILE *fp;
char p[100]="Welcome to Nepal";
fp=fopen("rec.txt","w");
fprintf(fp,"%s",p);
fclose(fp);
getch();
}

Homework Questions

C-6 (File Handling)

1. Write a program to enter name, post and salary of a employee and
write it in a file “employee.dat”?
2. Write a program to display name, age and address reading from file
“record.dat”.
3. Write a program which reads name, roll and age from a file
“student.dat” and display them?
4. Write which asks name, age and gender of student and write it in a file
“student.dat”.
5. Write a program which writes “Welcome to Nepal” in a file?