SlideShare a Scribd company logo
1 of 19
muzammal sadiq
FileFile
A file is a collection of related data stored
in
a particular area on the disk . The data is
stored in disk using the concept of file .
File Handling in C++
muzammal sadiq
Why FileWhy File
Permanent storage of data : - (all the message or
value printed with help of any output statements like
cout , putchar are never available for future use ) .
If there is a large amount of data generated as
output by a program, storing that output in file will
help in easy handling /analysis of the output , as user
can see the whole output at any time even after
complete execution of the program.
muzammal sadiq
If we need lot of data to be inputted, user cannot
keep on typing that again and again for repeated
execution of program. In that case, all input data can
be once written in a file and then that file can be
easily used as the input file.
The transfer of input – data or output – data from
one computer to another can be easily done by
using files.
muzammal sadiq
Stream classesStream classes
Filebuf :- its purpose is to set the file buffer to read and
write . Contain openprot constant used in the open()
of file stream classes . Also contain close() and open()
as method.
Fstreambase :- provides operations common to the file
stream. Serves as a base for fstream, ifstream and
ofsteram class. Contains open() and close() function
Ifstream :- provides input operations. Contains open()
with default input mode. Inherits the functions get(),
getline(), read(), seekg() and tellg() function from
istream.
muzammal sadiq
Ofstream :- provides output operations. Contains
open() with default output mode. Inherits put(),
seekp(), teelp() and write() function from
ostream.
Fstream :- provides support for simultaneous input
and output operations. Contains open() with default
input mode. Inherits all the function from isteram
and ostream classes through iostream
muzammal sadiq
Opening file using open()Opening file using open()
The function open() can be used to open
multiple files that use the same stream object.
file-stream-class stream-object;
stream-object . open (“filename”);
muzammal sadiq
Open and close a fileOpen and close a file
eg:-
ofstream outfile; // create stream
outfile . open (“DATA1”); // connect stream to DATA1
……………………………..
……………………………..
outfile . Close(); //disconnect stream from DATA1
outfile . Open(“DATA2”); //connect stream to DATA2
……………………………..
……………………………..
outfile . close();
……………………………..
muzammal sadiq
Mode of file openingMode of file opening
ios :: out = open file for write only
ios :: in = open file for read only
ios :: app = append to end-of-file
ios :: ate = take us to the end of the file when it
is opened
Both ios :: app and ios :: ate take us to the end of the file
when it is opened. The difference between the two
parameters is that the ios :: app allows us to add data to
the end of file only, while ios :: ate mode permits us to
add
data or to modify the existing data any where in the file.
muzammal sadiq
The mode can combine two or more parameters
using the bitwise OR operator (symbol |)
eg :-
fstream file;
file . Open(“ data . txt”, ios :: out | ios :: in);
muzammal sadiq
File pointerFile pointer
Each file have two associated pointers known as
the file pointers. One of them is called the input
pointer (or get pointer) and the other is called the
output pointer (or put pointer). The input pointer
is used for reading the contents of a given file
location and the output pointer is used for writing
to a given file location.
muzammal sadiq
Function for manipulationFunction for manipulation
of file pointerof file pointer
When we want to move file pointer to desired
position then use these function for manage the
file pointers.
Seekg () = moves get pointer (input) to a
specified location
Seekp () = moves put pointer (output) to a
specified location
tellg () = gives the current position of the get pointer
tellp () = gives the current position of the put pointer
muzammal sadiq
fout . seekg(0, ios :: beg) -- go to start
fout . seekg(0, ios :: cur) -- stay at current position
fout . seekg(0, ios :: end) -- go to the end of file
fout . seekg(m, ios :: beg) -- move to m+1 byte in the file
fout . seekg(m, ios :: cur) -- go forward by m bytes from
the current position
fout . seekg(-m, ios :: cur) -- go backward by m bytes
from the current position
fout . seekg(-m, ios :: end) -- go backward by m bytes
from the end
muzammal sadiq
put() and get() functionput() and get() function
The function put() write a single character to the
associated stream. Similarly, the function get()
reads a single character from the associated
stream.
muzammal sadiq
read() and write() functionread() and write() function
file . read ((char *)&V , sizeof (V));
file . Write ((char *)&V , sizeof (V));
These function take two arguments. The first is
the address of the variable V , and the second is
the length of that variable in bytes . The address
of variable must be cast to type char * (i.e pointer
to character type) .
muzammal sadiq
Program for file handlingProgram for file handling
#include< iostream . h>
#include< conio .h>
#include< fstream . h>
Class student
{
Public:
Struct stu
{
char name[20];
int roll;
}s;
Void put_data();
Void get_data();
};
muzammal sadiq
void student :: put_data()
{
cout<<"enter name ";
cin>>s. name;
cout<<"enter roll ";
cin>>s. roll;
file. Open ("hit. txt“ , ios :: out | ios :: app);
file. write ((char *)this, sizeof (student));
file. close();
getch();
get_data();
}
muzammal sadiq
void student :: get_data()
{
int temp;
cout<<"enter roll no. ";
cin >>temp;
fstream file;
file. open ("hit . txt", ios :: in);
file.seekg(0,ios::beg);
While (file . read ((char *) this, sizeof (student)))
{
If (temp==s. roll)
{
cout<<"student name "<< s . name<<"n";
cout<<"student roll "<< s . roll;
}
}
getch ();
}
muzammal sadiq
void main()
{
clrscr();
student st;
st .put_data();
}
muzammal sadiq

More Related Content

What's hot

20090109 Dsl2cpp Md Workbench
20090109 Dsl2cpp Md Workbench20090109 Dsl2cpp Md Workbench
20090109 Dsl2cpp Md Workbench
azubi
 
06 file processing
06 file processing06 file processing
06 file processing
Issay Meii
 

What's hot (20)

Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode Apache Scoop - Import with Append mode and Last Modified mode
Apache Scoop - Import with Append mode and Last Modified mode
 
Classification & Analysis of Unstructured Data
Classification & Analysis of Unstructured DataClassification & Analysis of Unstructured Data
Classification & Analysis of Unstructured Data
 
Automata Invasion
Automata InvasionAutomata Invasion
Automata Invasion
 
C++ tokens and expressions
C++ tokens and expressionsC++ tokens and expressions
C++ tokens and expressions
 
Java Week4(B) Notepad
Java Week4(B)   NotepadJava Week4(B)   Notepad
Java Week4(B) Notepad
 
File mangement
File mangementFile mangement
File mangement
 
Text tagging with finite state transducers
Text tagging with finite state transducersText tagging with finite state transducers
Text tagging with finite state transducers
 
Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathur
 
File
FileFile
File
 
Map Reduce Execution Architecture
Map Reduce Execution Architecture Map Reduce Execution Architecture
Map Reduce Execution Architecture
 
20090109 Dsl2cpp Md Workbench
20090109 Dsl2cpp Md Workbench20090109 Dsl2cpp Md Workbench
20090109 Dsl2cpp Md Workbench
 
06 file processing
06 file processing06 file processing
06 file processing
 
Java Week4(C) Notepad
Java Week4(C)   NotepadJava Week4(C)   Notepad
Java Week4(C) Notepad
 
Apache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathurApache pig presentation_siddharth_mathur
Apache pig presentation_siddharth_mathur
 
odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)odoo 11.0 development (CRUD)
odoo 11.0 development (CRUD)
 
Gur1009
Gur1009Gur1009
Gur1009
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
A compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCoreA compact bytecode format for JavaScriptCore
A compact bytecode format for JavaScriptCore
 

Similar to cpp-file-handling

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
 
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
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
Deepak Singh
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 

Similar to cpp-file-handling (20)

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
 
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
 
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 C++File Handling In C++
File Handling In C++
 
Chapter28 data-file-handling
Chapter28 data-file-handlingChapter28 data-file-handling
Chapter28 data-file-handling
 
Data file handling
Data file handlingData file handling
Data file handling
 
File Handling
File HandlingFile Handling
File Handling
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
Files in c++
Files in c++Files in c++
Files in c++
 
File Handling in C Programming
File Handling in C ProgrammingFile Handling in C Programming
File Handling in C Programming
 

cpp-file-handling

  • 2. FileFile A file is a collection of related data stored in a particular area on the disk . The data is stored in disk using the concept of file . File Handling in C++ muzammal sadiq
  • 3. Why FileWhy File Permanent storage of data : - (all the message or value printed with help of any output statements like cout , putchar are never available for future use ) . If there is a large amount of data generated as output by a program, storing that output in file will help in easy handling /analysis of the output , as user can see the whole output at any time even after complete execution of the program. muzammal sadiq
  • 4. If we need lot of data to be inputted, user cannot keep on typing that again and again for repeated execution of program. In that case, all input data can be once written in a file and then that file can be easily used as the input file. The transfer of input – data or output – data from one computer to another can be easily done by using files. muzammal sadiq
  • 5. Stream classesStream classes Filebuf :- its purpose is to set the file buffer to read and write . Contain openprot constant used in the open() of file stream classes . Also contain close() and open() as method. Fstreambase :- provides operations common to the file stream. Serves as a base for fstream, ifstream and ofsteram class. Contains open() and close() function Ifstream :- provides input operations. Contains open() with default input mode. Inherits the functions get(), getline(), read(), seekg() and tellg() function from istream. muzammal sadiq
  • 6. Ofstream :- provides output operations. Contains open() with default output mode. Inherits put(), seekp(), teelp() and write() function from ostream. Fstream :- provides support for simultaneous input and output operations. Contains open() with default input mode. Inherits all the function from isteram and ostream classes through iostream muzammal sadiq
  • 7. Opening file using open()Opening file using open() The function open() can be used to open multiple files that use the same stream object. file-stream-class stream-object; stream-object . open (“filename”); muzammal sadiq
  • 8. Open and close a fileOpen and close a file eg:- ofstream outfile; // create stream outfile . open (“DATA1”); // connect stream to DATA1 …………………………….. …………………………….. outfile . Close(); //disconnect stream from DATA1 outfile . Open(“DATA2”); //connect stream to DATA2 …………………………….. …………………………….. outfile . close(); …………………………….. muzammal sadiq
  • 9. Mode of file openingMode of file opening ios :: out = open file for write only ios :: in = open file for read only ios :: app = append to end-of-file ios :: ate = take us to the end of the file when it is opened Both ios :: app and ios :: ate take us to the end of the file when it is opened. The difference between the two parameters is that the ios :: app allows us to add data to the end of file only, while ios :: ate mode permits us to add data or to modify the existing data any where in the file. muzammal sadiq
  • 10. The mode can combine two or more parameters using the bitwise OR operator (symbol |) eg :- fstream file; file . Open(“ data . txt”, ios :: out | ios :: in); muzammal sadiq
  • 11. File pointerFile pointer Each file have two associated pointers known as the file pointers. One of them is called the input pointer (or get pointer) and the other is called the output pointer (or put pointer). The input pointer is used for reading the contents of a given file location and the output pointer is used for writing to a given file location. muzammal sadiq
  • 12. Function for manipulationFunction for manipulation of file pointerof file pointer When we want to move file pointer to desired position then use these function for manage the file pointers. Seekg () = moves get pointer (input) to a specified location Seekp () = moves put pointer (output) to a specified location tellg () = gives the current position of the get pointer tellp () = gives the current position of the put pointer muzammal sadiq
  • 13. fout . seekg(0, ios :: beg) -- go to start fout . seekg(0, ios :: cur) -- stay at current position fout . seekg(0, ios :: end) -- go to the end of file fout . seekg(m, ios :: beg) -- move to m+1 byte in the file fout . seekg(m, ios :: cur) -- go forward by m bytes from the current position fout . seekg(-m, ios :: cur) -- go backward by m bytes from the current position fout . seekg(-m, ios :: end) -- go backward by m bytes from the end muzammal sadiq
  • 14. put() and get() functionput() and get() function The function put() write a single character to the associated stream. Similarly, the function get() reads a single character from the associated stream. muzammal sadiq
  • 15. read() and write() functionread() and write() function file . read ((char *)&V , sizeof (V)); file . Write ((char *)&V , sizeof (V)); These function take two arguments. The first is the address of the variable V , and the second is the length of that variable in bytes . The address of variable must be cast to type char * (i.e pointer to character type) . muzammal sadiq
  • 16. Program for file handlingProgram for file handling #include< iostream . h> #include< conio .h> #include< fstream . h> Class student { Public: Struct stu { char name[20]; int roll; }s; Void put_data(); Void get_data(); }; muzammal sadiq
  • 17. void student :: put_data() { cout<<"enter name "; cin>>s. name; cout<<"enter roll "; cin>>s. roll; file. Open ("hit. txt“ , ios :: out | ios :: app); file. write ((char *)this, sizeof (student)); file. close(); getch(); get_data(); } muzammal sadiq
  • 18. void student :: get_data() { int temp; cout<<"enter roll no. "; cin >>temp; fstream file; file. open ("hit . txt", ios :: in); file.seekg(0,ios::beg); While (file . read ((char *) this, sizeof (student))) { If (temp==s. roll) { cout<<"student name "<< s . name<<"n"; cout<<"student roll "<< s . roll; } } getch (); } muzammal sadiq
  • 19. void main() { clrscr(); student st; st .put_data(); } muzammal sadiq