SlideShare a Scribd company logo
1 of 16
Download to read offline
KURUVA KARUN
PGT COMP SCIENCE
KVNO.2 VASCO
BANGALORE REGION
Understanding the basic concept of file handling.
Types of files.
Functions associated to operate on binary files.
Deleting a record from binary file.
2All rights reserved
 Computer programs are associated to work
with files as it helps in storing data &
information permanently.
 File - itself a bunch of bytes stored on some
storage devices.
 In C++ this is achieved through a component
header file called fstream.h
 The I/O library manages two aspects- as
interface and for transfer of data.
 The library predefine a set of operations for
all file related handling through certain
classes.
 stream is a general term used to name flow of
data.
Streams act as an interface between files and
programs.
A Stream is sequence of bytes.
They represent as a sequence of bytes and
deals with the flow of data.
Every stream is associated with a class having
member functions and operations for a
particular kind of data flow.
File  Program ( Input stream) - reads
Program  File (Output stream) – write
All designed into fstream.h and hence needs to
be included in all file handling programs.
A File can be stored in two ways
Text File
Binary File
Text Files : Stores information in ASCII characters. In text file
each line of text is terminated by with special character known
as EOL (End of Line) In text file some translations takes place
when this EOL character is read or written.
Binary File: it contains the information in the same format as
it is held in the memory. In binary file there is no delimiter for a
line. Also no translation occur in binary file. As a result binary
files are faster and easier for program to read and write.
Binary File Functions
Note: Both functions take two arguments.
• The first is the address of variable, and the second is the
length of that variable in bytes. The address of variable must
be type cast to type char*(pointer to character type)
• The data written to a file using write( ) can only be read
accurately using read( ).
Program to create a binary file ‘student.dat’ using structure.
#include<fstream.h>
struct student
{
char name[15];
float percent;
};
void main()
{
ofstream fout;
char ch;
fout.open(“student.dat”, ios::out | ios:: binary);
clrscr();
student s;
if(!fout)
{
cout<<“File can’t be opened”;
exit(0);
}
do
{ cout<<”n
enter name of student”;
gets(s.name);
cout<<”n enter percentage”;
cin>>s.percent;
fout.write((char *)&s,sizeof(s)); // writing a record in a student.dat file
cout<<”n more record y/n”;
cin>>ch;
}while(ch!=’n’ || ch!=’N’);
fout.close();
}
Deleting a Record from already existing file :
Algorithm :
 Assume the file already existed with the student structure.
(Each record contains percentage and name).
Declare a ifstream object and make it point to the existing
binary file student.dat and open it using input mode.
Ask the user to input the name of the student whose record
to be deleted. Store it in a String variable.
Declare a ofstream object and make it point to temporary
file (temp.dat) and open it in output mode.
Read each record from file and store it in a structure object.
(this can be achieved using read() ).
Compare the name of the record read into object and name
of record to be deleted .
CONTD…..
Deleting a Record from already existing file :
If both are not matching write the contents of the object into
the temporary file using write().
If both are matching skip the writing into the file.
Repeat the process till all the records are traversed
Close the connections associated with the both the objects
Delete the file (student.dat) using remove()
Rename the temporary file to the original file name.
Note : The process is same for text files also but the binary
modes are not needed. Read data line by line and compare
with target data to be deleted. Skip writing to temporary file if
both are matching.
Program to delete a record from a binary file
‘student.dat’
#include<fstream.h>
struct student
{
char name[15];
float percent;
};
void main()
{
ifstream fin;
student s;
char dname[15]; //to read the name of the record to delete
fin.open(“student.dat”,ios::in | ios:: binary);
ofstream fout(“temp.dat”,ios::out|ios::binary);
cout<<“n Enter name of the student whose record to be
deleted”;
cin>>dname;
while(fin.read((char *) &s, sizeof(student))
{
if(strcmp(s.name,dname)!=0)
fout.write((char *) &s, sizeof(s));
}
fin.close();
fout.close();
remove(“student.dat”);
rename(“temp.dat”,”student.dat”);
getch();
}
File Pointer
seekp(): It places the file pointer to the specified position in
output mode of file.
e.g file.seekp(p,ios::beg); or file.seekp(-p,ios::end), or
file.seekp(p,ios::cur)
i.e to move to p byte position from beginning, end or current
position.
tellg(): This function returns the current working position of the
file pointer in the input mode.
e.g int p=file.tellg();
tellp(): This function returns the current working position of the
file pointer in the output mode.
e.f int p=file.tellp();
Deletion of a Record from a File - K Karun
Deletion of a Record from a File - K Karun

More Related Content

What's hot (19)

CS215 - Lec 2 file organization
CS215 - Lec 2   file organizationCS215 - Lec 2   file organization
CS215 - Lec 2 file organization
 
Data file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing filesData file handling in python introduction,opening & closing files
Data file handling in python introduction,opening & closing files
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
file management functions
file management functionsfile management functions
file management functions
 
Javaio
JavaioJavaio
Javaio
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
File handling
File handlingFile handling
File handling
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
 
data file handling
data file handlingdata file handling
data file handling
 
C files
C filesC files
C files
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
 
Managing console of I/o operations & working with files
Managing console of I/o operations & working with filesManaging console of I/o operations & working with files
Managing console of I/o operations & working with files
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
File handling
File handlingFile handling
File handling
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
File handling
File handlingFile handling
File handling
 

Viewers also liked

Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashingAmi Ranjit
 
Lecture.extendible.hashing
Lecture.extendible.hashingLecture.extendible.hashing
Lecture.extendible.hashingChace Liang
 
Drawback of Internet of Things - Battery Life
Drawback of Internet of Things - Battery LifeDrawback of Internet of Things - Battery Life
Drawback of Internet of Things - Battery LifeAdithya Jayaprakash
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
Database intro
Database introDatabase intro
Database introKara Ross
 
Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Vidyasagar Mundroy
 
Database management system1
Database management system1Database management system1
Database management system1jamwal85
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-fileDeepak Singh
 
Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagramMayank Jain
 
DBMS topics for BCA
DBMS topics for BCADBMS topics for BCA
DBMS topics for BCAAdbay
 
1 introduction databases and database users
1 introduction databases and database users1 introduction databases and database users
1 introduction databases and database usersKumar
 
Database indexing techniques
Database indexing techniquesDatabase indexing techniques
Database indexing techniquesahmadmughal0312
 
File organization techniques
File organization techniquesFile organization techniques
File organization techniquesMeghlal Khan
 

Viewers also liked (20)

Indexing and-hashing
Indexing and-hashingIndexing and-hashing
Indexing and-hashing
 
Normal forms
Normal formsNormal forms
Normal forms
 
Lecture.extendible.hashing
Lecture.extendible.hashingLecture.extendible.hashing
Lecture.extendible.hashing
 
Chapter13
Chapter13Chapter13
Chapter13
 
Drawback of Internet of Things - Battery Life
Drawback of Internet of Things - Battery LifeDrawback of Internet of Things - Battery Life
Drawback of Internet of Things - Battery Life
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
Database intro
Database introDatabase intro
Database intro
 
Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)
 
Filehandling
FilehandlingFilehandling
Filehandling
 
Chapter2
Chapter2Chapter2
Chapter2
 
Database management system1
Database management system1Database management system1
Database management system1
 
Databse management system
Databse management systemDatabse management system
Databse management system
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagram
 
Er diagrams presentation
Er diagrams presentationEr diagrams presentation
Er diagrams presentation
 
DBMS topics for BCA
DBMS topics for BCADBMS topics for BCA
DBMS topics for BCA
 
Indexing Data Structure
Indexing Data StructureIndexing Data Structure
Indexing Data Structure
 
1 introduction databases and database users
1 introduction databases and database users1 introduction databases and database users
1 introduction databases and database users
 
Database indexing techniques
Database indexing techniquesDatabase indexing techniques
Database indexing techniques
 
File organization techniques
File organization techniquesFile organization techniques
File organization techniques
 

Similar to Deletion of a Record from a File - K Karun

chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfstudy material
 
File handling in_c
File handling in_cFile handling in_c
File handling in_csanya6900
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handlingpinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))Papu Kumar
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with exampleSunil Patel
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part IArpana Awasthi
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfsudhakargeruganti
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxAssadLeo1
 

Similar to Deletion of a Record from a File - K Karun (20)

7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
 
Data file handling
Data file handlingData file handling
Data file handling
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
File management in C++
File management in C++File management in C++
File management in C++
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Basics of files and its functions with example
Basics of files and its functions with exampleBasics of files and its functions with example
Basics of files and its functions with example
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Files in c++
Files in c++Files in c++
Files in c++
 
File Organization
File OrganizationFile Organization
File Organization
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 

More from Dipayan Sarkar

introduction to aep arc
introduction to aep arcintroduction to aep arc
introduction to aep arcDipayan Sarkar
 
why focus on adolescents unique needs
why focus on adolescents unique needswhy focus on adolescents unique needs
why focus on adolescents unique needsDipayan Sarkar
 
aep in india milestones, learning, way forward
aep in india milestones, learning, way forwardaep in india milestones, learning, way forward
aep in india milestones, learning, way forwardDipayan Sarkar
 
Softskills - S Fernandez
Softskills - S FernandezSoftskills - S Fernandez
Softskills - S FernandezDipayan Sarkar
 
Project Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K ChaurdiaProject Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K ChaurdiaDipayan Sarkar
 
Linux and the Open Source- D Sarkar
Linux and the Open Source- D SarkarLinux and the Open Source- D Sarkar
Linux and the Open Source- D SarkarDipayan Sarkar
 
File Handling - N K Upadhyay
File Handling - N K UpadhyayFile Handling - N K Upadhyay
File Handling - N K UpadhyayDipayan Sarkar
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan SatishSorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan SatishDipayan Sarkar
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
Information Technology Act 2000 - Santosh K Pathak
Information Technology Act 2000 - Santosh K PathakInformation Technology Act 2000 - Santosh K Pathak
Information Technology Act 2000 - Santosh K PathakDipayan Sarkar
 
Universal Gates - Aneesa N Ali
Universal Gates - Aneesa N AliUniversal Gates - Aneesa N Ali
Universal Gates - Aneesa N AliDipayan Sarkar
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin RamolaDipayan Sarkar
 
Java Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex JoseJava Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex JoseDipayan Sarkar
 
Computer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan ChakravarthyComputer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan ChakravarthyDipayan Sarkar
 
Cloud Computing- Sreehari S
Cloud Computing- Sreehari SCloud Computing- Sreehari S
Cloud Computing- Sreehari SDipayan Sarkar
 

More from Dipayan Sarkar (18)

Relationships
RelationshipsRelationships
Relationships
 
ideal learning space
ideal learning spaceideal learning space
ideal learning space
 
introduction to aep arc
introduction to aep arcintroduction to aep arc
introduction to aep arc
 
why focus on adolescents unique needs
why focus on adolescents unique needswhy focus on adolescents unique needs
why focus on adolescents unique needs
 
aep in india milestones, learning, way forward
aep in india milestones, learning, way forwardaep in india milestones, learning, way forward
aep in india milestones, learning, way forward
 
Softskills - S Fernandez
Softskills - S FernandezSoftskills - S Fernandez
Softskills - S Fernandez
 
Project Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K ChaurdiaProject Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K Chaurdia
 
Linux and the Open Source- D Sarkar
Linux and the Open Source- D SarkarLinux and the Open Source- D Sarkar
Linux and the Open Source- D Sarkar
 
File Handling - N K Upadhyay
File Handling - N K UpadhyayFile Handling - N K Upadhyay
File Handling - N K Upadhyay
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan SatishSorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan Satish
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Information Technology Act 2000 - Santosh K Pathak
Information Technology Act 2000 - Santosh K PathakInformation Technology Act 2000 - Santosh K Pathak
Information Technology Act 2000 - Santosh K Pathak
 
Universal Gates - Aneesa N Ali
Universal Gates - Aneesa N AliUniversal Gates - Aneesa N Ali
Universal Gates - Aneesa N Ali
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
 
Java Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex JoseJava Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex Jose
 
Computer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan ChakravarthyComputer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan Chakravarthy
 
Cloud Computing- Sreehari S
Cloud Computing- Sreehari SCloud Computing- Sreehari S
Cloud Computing- Sreehari S
 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Jason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentJason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentInMediaRes1
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...Nguyen Thanh Tu Collection
 
16. Discovery, function and commercial uses of different PGRS.pptx
16. Discovery, function and commercial uses of different PGRS.pptx16. Discovery, function and commercial uses of different PGRS.pptx
16. Discovery, function and commercial uses of different PGRS.pptxUmeshTimilsina1
 
How to create _name_search function in odoo 17
How to create _name_search function in odoo 17How to create _name_search function in odoo 17
How to create _name_search function in odoo 17Celine George
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Farrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest EntranceFarrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest Entrancejulius27264
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...Nguyen Thanh Tu Collection
 
Paul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentPaul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentInMediaRes1
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
Vinícius Portella In Media Res Media Component
Vinícius Portella In Media Res Media ComponentVinícius Portella In Media Res Media Component
Vinícius Portella In Media Res Media ComponentInMediaRes1
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsArubSultan
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
Jason Potel In Media Res Media Component
Jason Potel In Media Res Media ComponentJason Potel In Media Res Media Component
Jason Potel In Media Res Media Component
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
BÀI TẬP BỔ TRỢ 4 KĨ NĂNG TIẾNG ANH LỚP 11 (CẢ NĂM) - FRIENDS GLOBAL - NĂM HỌC...
 
16. Discovery, function and commercial uses of different PGRS.pptx
16. Discovery, function and commercial uses of different PGRS.pptx16. Discovery, function and commercial uses of different PGRS.pptx
16. Discovery, function and commercial uses of different PGRS.pptx
 
How to create _name_search function in odoo 17
How to create _name_search function in odoo 17How to create _name_search function in odoo 17
How to create _name_search function in odoo 17
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Farrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest EntranceFarrington HS Streamlines Guest Entrance
Farrington HS Streamlines Guest Entrance
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
CHUYÊN ĐỀ ÔN THEO CÂU CHO HỌC SINH LỚP 12 ĐỂ ĐẠT ĐIỂM 5+ THI TỐT NGHIỆP THPT ...
 
Paul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media ComponentPaul Dobryden In Media Res Media Component
Paul Dobryden In Media Res Media Component
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
Vinícius Portella In Media Res Media Component
Vinícius Portella In Media Res Media ComponentVinícius Portella In Media Res Media Component
Vinícius Portella In Media Res Media Component
 
Shark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristicsShark introduction Morphology and its behaviour characteristics
Shark introduction Morphology and its behaviour characteristics
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 

Deletion of a Record from a File - K Karun

  • 1. KURUVA KARUN PGT COMP SCIENCE KVNO.2 VASCO BANGALORE REGION
  • 2. Understanding the basic concept of file handling. Types of files. Functions associated to operate on binary files. Deleting a record from binary file. 2All rights reserved
  • 3.  Computer programs are associated to work with files as it helps in storing data & information permanently.  File - itself a bunch of bytes stored on some storage devices.  In C++ this is achieved through a component header file called fstream.h  The I/O library manages two aspects- as interface and for transfer of data.  The library predefine a set of operations for all file related handling through certain classes.
  • 4.  stream is a general term used to name flow of data. Streams act as an interface between files and programs. A Stream is sequence of bytes. They represent as a sequence of bytes and deals with the flow of data. Every stream is associated with a class having member functions and operations for a particular kind of data flow. File  Program ( Input stream) - reads Program  File (Output stream) – write All designed into fstream.h and hence needs to be included in all file handling programs.
  • 5.
  • 6. A File can be stored in two ways Text File Binary File Text Files : Stores information in ASCII characters. In text file each line of text is terminated by with special character known as EOL (End of Line) In text file some translations takes place when this EOL character is read or written. Binary File: it contains the information in the same format as it is held in the memory. In binary file there is no delimiter for a line. Also no translation occur in binary file. As a result binary files are faster and easier for program to read and write.
  • 7. Binary File Functions Note: Both functions take two arguments. • The first is the address of variable, and the second is the length of that variable in bytes. The address of variable must be type cast to type char*(pointer to character type) • The data written to a file using write( ) can only be read accurately using read( ).
  • 8. Program to create a binary file ‘student.dat’ using structure. #include<fstream.h> struct student { char name[15]; float percent; }; void main() { ofstream fout; char ch; fout.open(“student.dat”, ios::out | ios:: binary); clrscr(); student s; if(!fout) { cout<<“File can’t be opened”; exit(0); }
  • 9. do { cout<<”n enter name of student”; gets(s.name); cout<<”n enter percentage”; cin>>s.percent; fout.write((char *)&s,sizeof(s)); // writing a record in a student.dat file cout<<”n more record y/n”; cin>>ch; }while(ch!=’n’ || ch!=’N’); fout.close(); }
  • 10. Deleting a Record from already existing file : Algorithm :  Assume the file already existed with the student structure. (Each record contains percentage and name). Declare a ifstream object and make it point to the existing binary file student.dat and open it using input mode. Ask the user to input the name of the student whose record to be deleted. Store it in a String variable. Declare a ofstream object and make it point to temporary file (temp.dat) and open it in output mode. Read each record from file and store it in a structure object. (this can be achieved using read() ). Compare the name of the record read into object and name of record to be deleted . CONTD…..
  • 11. Deleting a Record from already existing file : If both are not matching write the contents of the object into the temporary file using write(). If both are matching skip the writing into the file. Repeat the process till all the records are traversed Close the connections associated with the both the objects Delete the file (student.dat) using remove() Rename the temporary file to the original file name. Note : The process is same for text files also but the binary modes are not needed. Read data line by line and compare with target data to be deleted. Skip writing to temporary file if both are matching.
  • 12. Program to delete a record from a binary file ‘student.dat’ #include<fstream.h> struct student { char name[15]; float percent; }; void main() { ifstream fin; student s; char dname[15]; //to read the name of the record to delete fin.open(“student.dat”,ios::in | ios:: binary); ofstream fout(“temp.dat”,ios::out|ios::binary); cout<<“n Enter name of the student whose record to be deleted”; cin>>dname;
  • 13. while(fin.read((char *) &s, sizeof(student)) { if(strcmp(s.name,dname)!=0) fout.write((char *) &s, sizeof(s)); } fin.close(); fout.close(); remove(“student.dat”); rename(“temp.dat”,”student.dat”); getch(); }
  • 14. File Pointer seekp(): It places the file pointer to the specified position in output mode of file. e.g file.seekp(p,ios::beg); or file.seekp(-p,ios::end), or file.seekp(p,ios::cur) i.e to move to p byte position from beginning, end or current position. tellg(): This function returns the current working position of the file pointer in the input mode. e.g int p=file.tellg(); tellp(): This function returns the current working position of the file pointer in the output mode. e.f int p=file.tellp();