SlideShare a Scribd company logo
FILE HANDLING IN
C++
Files (Streams)
Files are used to store data in a relatively
permanent form, on floppy disk, hard disk,
tape or other form of secondary storage.
Files can hold huge amounts of data if need
be. Ordinary variables (even records and
arrays) are kept in main memory which is
temporary and rather limited in size. The
following is a comparison of the two types
of storage:
Main memory
 Made up of RAM chips.
 Used to hold a program
when it is running,
including the values of its
variables (whether
integer, char, an array,
etc.)
 Can only hold relatively
small amounts of data.
 Is temporary (as soon as
the program is done or the
power goes out all of
these values are gone).
 Gives fast access to the
data (all electronic).
Secondary memory
 Usually a disk drive (or magnetic
tape).
 Used to hold files (where a file
can contain data, a program, text,
etc.)
 Can hold rather large amounts of
data.
 Is fairly permanent. (A file
remains even if the power goes
out. It will last until you erase it,
as long as the disk isn't damaged,
at least.)
• Access to the data is
considerably slower (due to
moving parts).
C++ STREAMS
A Stream is a general name given to flow of
data.
Different streams are used to represent
different kinds of data flow.
Each stream is associated with a particular
class, which contains member functions and
definitions for dealing with that particular
kind of data flow.
Flow of Data….
PROGRAM
DEVICES OR
FILES
Input
Stream
>>
Output
Stream
<<
Data
Data
istream class ostream class
(Insertion
operator)
(Extraction
operator)
The following classes in C++
have access to file input and
output functions:
ifstream
ofstream
fstream
The Stream Class Hierarchy
ios
istream
get()
getline()
read()
>>
ostream
put()
write()
<<
fstreambase
iostream
Ifstream
Open()
Tellg()
Seekg()
Ofstream
Open()
Tellp()
Seekp()
fstream
NOTE : UPWARD ARROWS INDICATE
THE BASE CLASS
DIFFERENT FILE OPERATIONS
OPENING A FILE
CLOSING A FILE
READING FROM A FILE
WRITING ON A FILE
CHECKING FOR END OF FILE
OPENING A FILE
1. By using the CONSTRUCTOR of the
stream class.
ifstream transaction(“sales.dly”);
ofstream result(“result.02”);
2. By using the open() function of the stream
class
ifstream transaction;
transaction.open(“sales.dly”);
(Associating a stream with a file)
File Mode Parameters
PARAMETER MEANING
Ios::app Append to end-of file
Ios::ate goto end of file on opening
Ios::binary binary file
Ios::in Open existing file for reading
Ios::nocreate open fails if file doesn’t exist
Ios::noreplace open fails if file already exists
Ios::out creates new file for writing on
Ios::trunc Deletes contents if it exists
The mode can combine two or more modes using bit wise
or ( | )
Checking For Successful File Opening
ifstream transaction(“sales.dly”);
if (transcation == NULL)
{
cout<<“unable to open sales.dly”;
cin.get(); // waits for the operator to press any key
exit(1);
}
Closing of File
Stream_name.close();
e.g., transaction.close();
Types of Files
. The two basic types are
– text and
– binary.
A text file consists of readable characters
separated into lines by newline characters.
(On most PCs, the newline character is
actually represented by the two-character
sequence of carriage return (ASCII 13), line
feed (ASCII 10).
A binary file stores data to disk in the same
form in which it is represented in main
memory.
If you ever try to edit a binary file containing
numbers you will see that the numbers
appear as nonsense characters. Not having to
translate numbers into a readable form makes
binary files somewhat more efficient.
Binary files also do not normally use
anything to separate the data into lines. Such
a file is just a stream of data with nothing in
particular to separate components.
When using a binary file we write whole
record data to the file at once. When using a
text file, we write out separately each of the
pieces of data about a given record.
The text file will be readable by an editor,
but the numbers in the binary file will not
be readable in this way.
The programs to create the data files will
differ in how they open the file and in how
they write to the file.
For the binary file we will use write to
write to the file, whereas for the text file we
will use the usual output operator(<<) and
will output each of the pieces of the record
separately.
With the binary file we will use the read
function to read a whole record, but with
the text file we will read each of the pieces
of record from the file separately, using the
usual input operator(>>)
EXAMPLES
 Creation of a text file
:
Sequential access. With this type of file
access one must read the data in order,
much like with a tape, whether the data is
really stored on tape or not.
Random access (or direct access). This
type of file access lets you jump to any
location in the file, then to any other, etc.,
all in a reasonable amount of time.
Types of File Access
FILE POINTERS
FILE POINTERS
Each file object has two integer values
associated with it :
– get pointer
– put pointer
These values specify the byte number in the
file where reading or writing will take
place.
File pointers…..
By default reading pointer is set at the
beginning and writing pointer is set at the
end (when you open file in ios::app mode)
There are times when you must take control
of the file pointers yourself so that you can
read from and write to an arbitrary location
in the file.
Functions associated with file
pointers :
The seekg() and tellg() functions allow you
to set and examine the get pointer.
The seekp() and tellp() functions allow you
to set and examine the put pointer.
seekg() function :
With one argument :
seekg(k) where k is absolute position from
the beginning. The start of the file is byte 0
Begin File End
k bytes ^
File pointer
The seekg() function with one argument
seekg() function :
With two arguments :
the first argument represents an offset from a particular
location in the file.
the second specifies the location from which the offset is
measured.
Begin End
^
Offset from Begin
The seekg() function with two argument
seekg() function :
With two arguments :
Begin End
^
Offset from Begin
The seekg() function with two argument
^
^
Offset from end
Offset from current
position
//
#include <fstream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
//clrscr();
char c,d,ans;
char str[80];
ofstream outfl("try.txt"),out("cod.dat");
ifstream infl;
do
{ cout<<"please give the string : ";
gets(str);
outfl<<str;
cout <<"do you want to write more...<y/n> : ";
ans=getch();
}
while(ans=='y');
outfl<<'0';
outfl.close();
//clrscr();
getch();
cout <<"reading from created file n";
infl.open("try.txt");
out.open("cod.dat");
//**********************************
c=infl.get();
do
{ d=c+1;
cout<<c<<d<<'n';
out.put(d);
c= infl.get();
}
while (c!='0');
out<<'0';
infl.close();
outfl.close();
getch();
//*********************************
}
Filepointers1 1215104829397318-9

More Related Content

What's hot

File handling in c++
File handling in c++File handling in c++
File handling in c++
ProfSonaliGholveDoif
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
Papu Kumar
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
Hitesh Kumar
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
keeeerty
 
Files in c++
Files in c++Files in c++
Files in c++
NivethaJeyaraman
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
Lifna C.S
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Files and file objects (in Python)
Files and file objects (in Python)Files and file objects (in Python)
Files and file objects (in Python)
PranavSB
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
Ahmed Farag
 
File handling
File handlingFile handling
File handling
BeebashPokhrel
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
Megha V
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
Docent Education
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
gourav kottawar
 
python file handling
python file handlingpython file handling
python file handling
jhona2z
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
Dr .Ahmed Tawwab
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
JiaahRajpout123
 
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
 

What's hot (20)

File handling in c++
File handling in c++File handling in c++
File handling in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
File handling in C++
File handling in C++File handling in C++
File handling in C++
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
Files in c++
Files in c++Files in c++
Files in c++
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Files and file objects (in Python)
Files and file objects (in Python)Files and file objects (in Python)
Files and file objects (in Python)
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
File handling
File handlingFile handling
File handling
 
File handling in Python
File handling in PythonFile handling in Python
File handling in Python
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
python file handling
python file handlingpython file handling
python file handling
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
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
 

Similar to Filepointers1 1215104829397318-9

new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
File Handling
File HandlingFile Handling
File Handling
TusharBatra27
 
Data file handling
Data file handlingData file handling
Data file handling
Prof. Dr. K. Adisesha
 
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
File handling in_cFile handling in_c
File handling in_c
sanya6900
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
ChereLemma2
 
File management in C++
File management in C++File management in C++
File management in C++
apoorvaverma33
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
Karudaiyar Ganapathy
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
Osmania University
 
File Handling in c++
File Handling in c++File Handling in c++
File Handling in c++
SoniKirtan
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
Praveen M Jigajinni
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
MikialeTesfamariam
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
vsol7206
 
File_handling in c++ and its use cases.pptx
File_handling in c++ and its use cases.pptxFile_handling in c++ and its use cases.pptx
File_handling in c++ and its use cases.pptx
ZenLooper
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 
File Handling
File HandlingFile Handling
File Handling
AlgeronTongdoTopi
 

Similar to Filepointers1 1215104829397318-9 (20)

new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
 
File Handling
File HandlingFile Handling
File Handling
 
Data file handling
Data file handlingData file handling
Data file handling
 
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
File handling in_cFile handling in_c
File handling in_c
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 
File management in C++
File management in C++File management in C++
File management in C++
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
Python-FileHandling.pptx
Python-FileHandling.pptxPython-FileHandling.pptx
Python-FileHandling.pptx
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File Handling in c++
File Handling in c++File Handling in c++
File Handling in c++
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reugeFile handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
File handling3 (1).pdf uhgipughserigrfiogrehpiuhnfi;reuge
 
File_handling in c++ and its use cases.pptx
File_handling in c++ and its use cases.pptxFile_handling in c++ and its use cases.pptx
File_handling in c++ and its use cases.pptx
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 

Recently uploaded

Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
ossaicprecious19
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
muralinath2
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
ChetanK57
 
Structural Classification Of Protein (SCOP)
Structural Classification Of Protein  (SCOP)Structural Classification Of Protein  (SCOP)
Structural Classification Of Protein (SCOP)
aishnasrivastava
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
DiyaBiswas10
 
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
ssuserbfdca9
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
Scintica Instrumentation
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
sachin783648
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Sérgio Sacani
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
YOGESH DOGRA
 
In silico drugs analogue design: novobiocin analogues.pptx
In silico drugs analogue design: novobiocin analogues.pptxIn silico drugs analogue design: novobiocin analogues.pptx
In silico drugs analogue design: novobiocin analogues.pptx
AlaminAfendy1
 
platelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptxplatelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptx
muralinath2
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
subedisuryaofficial
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
Areesha Ahmad
 
EY - Supply Chain Services 2018_template.pptx
EY - Supply Chain Services 2018_template.pptxEY - Supply Chain Services 2018_template.pptx
EY - Supply Chain Services 2018_template.pptx
AlguinaldoKong
 
insect taxonomy importance systematics and classification
insect taxonomy importance systematics and classificationinsect taxonomy importance systematics and classification
insect taxonomy importance systematics and classification
anitaento25
 
GBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture MediaGBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture Media
Areesha Ahmad
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SELF-EXPLANATORY
 
Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
AADYARAJPANDEY1
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
Sérgio Sacani
 

Recently uploaded (20)

Lab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerinLab report on liquid viscosity of glycerin
Lab report on liquid viscosity of glycerin
 
platelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptxplatelets_clotting_biogenesis.clot retractionpptx
platelets_clotting_biogenesis.clot retractionpptx
 
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATIONPRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
PRESENTATION ABOUT PRINCIPLE OF COSMATIC EVALUATION
 
Structural Classification Of Protein (SCOP)
Structural Classification Of Protein  (SCOP)Structural Classification Of Protein  (SCOP)
Structural Classification Of Protein (SCOP)
 
extra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdfextra-chromosomal-inheritance[1].pptx.pdfpdf
extra-chromosomal-inheritance[1].pptx.pdfpdf
 
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
4. An Overview of Sugarcane White Leaf Disease in Vietnam.pdf
 
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
(May 29th, 2024) Advancements in Intravital Microscopy- Insights for Preclini...
 
Comparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebratesComparative structure of adrenal gland in vertebrates
Comparative structure of adrenal gland in vertebrates
 
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
Earliest Galaxies in the JADES Origins Field: Luminosity Function and Cosmic ...
 
Mammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also FunctionsMammalian Pineal Body Structure and Also Functions
Mammalian Pineal Body Structure and Also Functions
 
In silico drugs analogue design: novobiocin analogues.pptx
In silico drugs analogue design: novobiocin analogues.pptxIn silico drugs analogue design: novobiocin analogues.pptx
In silico drugs analogue design: novobiocin analogues.pptx
 
platelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptxplatelets- lifespan -Clot retraction-disorders.pptx
platelets- lifespan -Clot retraction-disorders.pptx
 
Citrus Greening Disease and its Management
Citrus Greening Disease and its ManagementCitrus Greening Disease and its Management
Citrus Greening Disease and its Management
 
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of LipidsGBSN - Biochemistry (Unit 5) Chemistry of Lipids
GBSN - Biochemistry (Unit 5) Chemistry of Lipids
 
EY - Supply Chain Services 2018_template.pptx
EY - Supply Chain Services 2018_template.pptxEY - Supply Chain Services 2018_template.pptx
EY - Supply Chain Services 2018_template.pptx
 
insect taxonomy importance systematics and classification
insect taxonomy importance systematics and classificationinsect taxonomy importance systematics and classification
insect taxonomy importance systematics and classification
 
GBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture MediaGBSN - Microbiology (Lab 4) Culture Media
GBSN - Microbiology (Lab 4) Culture Media
 
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdfSCHIZOPHRENIA Disorder/ Brain Disorder.pdf
SCHIZOPHRENIA Disorder/ Brain Disorder.pdf
 
Cancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate PathwayCancer cell metabolism: special Reference to Lactate Pathway
Cancer cell metabolism: special Reference to Lactate Pathway
 
Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...Multi-source connectivity as the driver of solar wind variability in the heli...
Multi-source connectivity as the driver of solar wind variability in the heli...
 

Filepointers1 1215104829397318-9

  • 2. Files (Streams) Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form of secondary storage. Files can hold huge amounts of data if need be. Ordinary variables (even records and arrays) are kept in main memory which is temporary and rather limited in size. The following is a comparison of the two types of storage:
  • 3. Main memory  Made up of RAM chips.  Used to hold a program when it is running, including the values of its variables (whether integer, char, an array, etc.)  Can only hold relatively small amounts of data.  Is temporary (as soon as the program is done or the power goes out all of these values are gone).  Gives fast access to the data (all electronic). Secondary memory  Usually a disk drive (or magnetic tape).  Used to hold files (where a file can contain data, a program, text, etc.)  Can hold rather large amounts of data.  Is fairly permanent. (A file remains even if the power goes out. It will last until you erase it, as long as the disk isn't damaged, at least.) • Access to the data is considerably slower (due to moving parts).
  • 4. C++ STREAMS A Stream is a general name given to flow of data. Different streams are used to represent different kinds of data flow. Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow.
  • 5. Flow of Data…. PROGRAM DEVICES OR FILES Input Stream >> Output Stream << Data Data istream class ostream class (Insertion operator) (Extraction operator)
  • 6. The following classes in C++ have access to file input and output functions: ifstream ofstream fstream
  • 7. The Stream Class Hierarchy ios istream get() getline() read() >> ostream put() write() << fstreambase iostream Ifstream Open() Tellg() Seekg() Ofstream Open() Tellp() Seekp() fstream NOTE : UPWARD ARROWS INDICATE THE BASE CLASS
  • 8. DIFFERENT FILE OPERATIONS OPENING A FILE CLOSING A FILE READING FROM A FILE WRITING ON A FILE CHECKING FOR END OF FILE
  • 9. OPENING A FILE 1. By using the CONSTRUCTOR of the stream class. ifstream transaction(“sales.dly”); ofstream result(“result.02”); 2. By using the open() function of the stream class ifstream transaction; transaction.open(“sales.dly”); (Associating a stream with a file)
  • 10. File Mode Parameters PARAMETER MEANING Ios::app Append to end-of file Ios::ate goto end of file on opening Ios::binary binary file Ios::in Open existing file for reading Ios::nocreate open fails if file doesn’t exist Ios::noreplace open fails if file already exists Ios::out creates new file for writing on Ios::trunc Deletes contents if it exists The mode can combine two or more modes using bit wise or ( | )
  • 11. Checking For Successful File Opening ifstream transaction(“sales.dly”); if (transcation == NULL) { cout<<“unable to open sales.dly”; cin.get(); // waits for the operator to press any key exit(1); }
  • 13. Types of Files . The two basic types are – text and – binary. A text file consists of readable characters separated into lines by newline characters. (On most PCs, the newline character is actually represented by the two-character sequence of carriage return (ASCII 13), line feed (ASCII 10).
  • 14. A binary file stores data to disk in the same form in which it is represented in main memory. If you ever try to edit a binary file containing numbers you will see that the numbers appear as nonsense characters. Not having to translate numbers into a readable form makes binary files somewhat more efficient. Binary files also do not normally use anything to separate the data into lines. Such a file is just a stream of data with nothing in particular to separate components.
  • 15. When using a binary file we write whole record data to the file at once. When using a text file, we write out separately each of the pieces of data about a given record. The text file will be readable by an editor, but the numbers in the binary file will not be readable in this way. The programs to create the data files will differ in how they open the file and in how they write to the file.
  • 16. For the binary file we will use write to write to the file, whereas for the text file we will use the usual output operator(<<) and will output each of the pieces of the record separately. With the binary file we will use the read function to read a whole record, but with the text file we will read each of the pieces of record from the file separately, using the usual input operator(>>)
  • 18. : Sequential access. With this type of file access one must read the data in order, much like with a tape, whether the data is really stored on tape or not. Random access (or direct access). This type of file access lets you jump to any location in the file, then to any other, etc., all in a reasonable amount of time. Types of File Access
  • 20. FILE POINTERS Each file object has two integer values associated with it : – get pointer – put pointer These values specify the byte number in the file where reading or writing will take place.
  • 21. File pointers….. By default reading pointer is set at the beginning and writing pointer is set at the end (when you open file in ios::app mode) There are times when you must take control of the file pointers yourself so that you can read from and write to an arbitrary location in the file.
  • 22. Functions associated with file pointers : The seekg() and tellg() functions allow you to set and examine the get pointer. The seekp() and tellp() functions allow you to set and examine the put pointer.
  • 23. seekg() function : With one argument : seekg(k) where k is absolute position from the beginning. The start of the file is byte 0 Begin File End k bytes ^ File pointer The seekg() function with one argument
  • 24. seekg() function : With two arguments : the first argument represents an offset from a particular location in the file. the second specifies the location from which the offset is measured. Begin End ^ Offset from Begin The seekg() function with two argument
  • 25. seekg() function : With two arguments : Begin End ^ Offset from Begin The seekg() function with two argument ^ ^ Offset from end Offset from current position
  • 26. // #include <fstream.h> #include <conio.h> #include <stdio.h> void main() { //clrscr(); char c,d,ans; char str[80]; ofstream outfl("try.txt"),out("cod.dat"); ifstream infl; do { cout<<"please give the string : "; gets(str); outfl<<str; cout <<"do you want to write more...<y/n> : "; ans=getch(); } while(ans=='y'); outfl<<'0'; outfl.close(); //clrscr(); getch(); cout <<"reading from created file n"; infl.open("try.txt"); out.open("cod.dat"); //********************************** c=infl.get(); do { d=c+1; cout<<c<<d<<'n'; out.put(d); c= infl.get(); } while (c!='0'); out<<'0'; infl.close(); outfl.close(); getch(); //********************************* }