SlideShare a Scribd company logo
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

CS215 - Lec 2 file organization
CS215 - Lec 2   file organizationCS215 - Lec 2   file organization
CS215 - Lec 2 file organization
Arab Open University and Cairo University
 
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
keeeerty
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
Tanmay Baranwal
 
file management functions
file management functionsfile management functions
file management functions
vaani pathak
 
Javaio
JavaioJavaio
Javaio
Jaya Jeswani
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
primeteacher32
 
File handling
File handlingFile handling
File handling
prateekgemini
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 
ASP.NET Session 7
ASP.NET Session 7ASP.NET Session 7
ASP.NET Session 7
Sisir Ghosh
 
data file handling
data file handlingdata file handling
data file handling
krishna partiwala
 
C files
C filesC files
Streams and Files
Streams and FilesStreams and Files
Streams and Files
Munazza-Mah-Jabeen
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
Prem Kumar Badri
 
Mesics lecture files in 'c'
Mesics lecture   files in 'c'Mesics lecture   files in 'c'
Mesics lecture files in 'c'
eShikshak
 
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
lalithambiga kamaraj
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
Debasish Pratihari
 
File handling
File handlingFile handling
File handling
Nilesh Dalvi
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
siragezeynu
 
File handling
File handlingFile handling
File handling
Amber Wajid
 

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-hashing
Ami Ranjit
 
Normal forms
Normal formsNormal forms
Normal forms
Samuel Igbanogu
 
Lecture.extendible.hashing
Lecture.extendible.hashingLecture.extendible.hashing
Lecture.extendible.hashing
Chace Liang
 
Chapter13
Chapter13Chapter13
Chapter13
gourab87
 
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
Adithya Jayaprakash
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 
Database intro
Database introDatabase intro
Database intro
Kara 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
 
Filehandling
FilehandlingFilehandling
Filehandling
Amandeep Kaur
 
Chapter2
Chapter2Chapter2
Chapter2
Bahcesehir Uni
 
Database management system1
Database management system1Database management system1
Database management system1
jamwal85
 
Databse management system
Databse management systemDatabse management system
Databse management system
Chittagong University
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
Deepak Singh
 
Enhanced E-R diagram
Enhanced E-R diagramEnhanced E-R diagram
Enhanced E-R diagram
Mayank Jain
 
Er diagrams presentation
Er diagrams presentationEr diagrams presentation
Er diagrams presentation
Akanksha Jaiswal
 
DBMS topics for BCA
DBMS topics for BCADBMS topics for BCA
DBMS topics for BCA
Adbay
 
Indexing Data Structure
Indexing Data StructureIndexing Data Structure
Indexing Data Structure
Vivek Kantariya
 
1 introduction databases and database users
1 introduction databases and database users1 introduction databases and database users
1 introduction databases and database users
Kumar
 
Database indexing techniques
Database indexing techniquesDatabase indexing techniques
Database indexing techniques
ahmadmughal0312
 
File organization techniques
File organization techniquesFile organization techniques
File organization techniques
Meghlal 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

7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
Praveen M Jigajinni
 
chapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdfchapter-12-data-file-handling.pdf
chapter-12-data-file-handling.pdf
study material
 
Data file handling
Data file handlingData file handling
Data file handling
Prof. Dr. K. Adisesha
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
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
sanya6900
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_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 Unit5
YOGESH 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 example
Sunil Patel
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
Mukesh Tekwani
 
File Handling in C Part I
File Handling in C Part IFile Handling in C Part I
File Handling in C Part I
Arpana 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.pdf
sudhakargeruganti
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Files in c++
Files in c++Files in c++
Files in c++
Selvin Josy Bai Somu
 
File Organization
File OrganizationFile Organization
File Organization
RAMPRAKASH REDDY ARAVA
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
HindAlmisbahi
 
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
AssadLeo1
 

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
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
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
 

More from Dipayan Sarkar

Relationships
RelationshipsRelationships
Relationships
Dipayan Sarkar
 
ideal learning space
ideal learning spaceideal learning space
ideal learning space
Dipayan Sarkar
 
introduction to aep arc
introduction to aep arcintroduction to aep arc
introduction to aep arc
Dipayan Sarkar
 
why focus on adolescents unique needs
why focus on adolescents unique needswhy focus on adolescents unique needs
why focus on adolescents unique needs
Dipayan 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 forward
Dipayan Sarkar
 
Softskills - S Fernandez
Softskills - S FernandezSoftskills - S Fernandez
Softskills - S Fernandez
Dipayan Sarkar
 
Project Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K ChaurdiaProject Based Learning- Ashish K Chaurdia
Project Based Learning- Ashish K Chaurdia
Dipayan 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 Sarkar
Dipayan Sarkar
 
File Handling - N K Upadhyay
File Handling - N K UpadhyayFile Handling - N K Upadhyay
File Handling - N K Upadhyay
Dipayan Sarkar
 
Sorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan SatishSorting Techniques - Govardhan Satish
Sorting Techniques - Govardhan Satish
Dipayan Sarkar
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
Dipayan 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 Pathak
Dipayan Sarkar
 
Universal Gates - Aneesa N Ali
Universal Gates - Aneesa N AliUniversal Gates - Aneesa N Ali
Universal Gates - Aneesa N Ali
Dipayan Sarkar
 
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
 
Java Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex JoseJava Databse Connectvity- Alex Jose
Java Databse Connectvity- Alex Jose
Dipayan Sarkar
 
Computer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan ChakravarthyComputer Viruses- B S Kalyan Chakravarthy
Computer Viruses- B S Kalyan Chakravarthy
Dipayan Sarkar
 
Cloud Computing- Sreehari S
Cloud Computing- Sreehari SCloud Computing- Sreehari S
Cloud Computing- Sreehari S
Dipayan Sarkar
 
SQL JOINS- Reena P V
SQL JOINS- Reena P VSQL JOINS- Reena P V
SQL JOINS- Reena P V
Dipayan 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

Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
chanes7
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
Dr. Mulla Adam Ali
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 

Recently uploaded (20)

Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
Digital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments UnitDigital Artifact 1 - 10VCD Environments Unit
Digital Artifact 1 - 10VCD Environments Unit
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Hindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdfHindi varnamala | hindi alphabet PPT.pdf
Hindi varnamala | hindi alphabet PPT.pdf
 
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 

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();