SlideShare a Scribd company logo
1 of 21
Data File Handling
Introduction
• 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.hfstream.h
Contd…
• Earlier, only two streams are being used such as cin
and cout.
• There are some problems with these streams.
1)It is very difficult to handle large volume of data.
2)The data is not permanently stored.
Eg:- the calculation of the result of 200 students will
be lost when the program terminates.
Contd…..
• C++ provides a new technique for handling I/O
operations through mechanism known as streams.
• A stream refers to a flow of data.
• Classified in 2 categories:
1. Output stream
2. Input stream
In output stream flow of data is from program to the
output device.
In input stream the flow of data is from input device
to a program in main memory.
File  Program ( Input stream) - reads
Program  File (Output stream) – write
Stream Class Hierarchy
Why to use Files:
• Convenient way to deal large quantities of data.
• Store data permanently (until file is deleted).
• Avoid typing data into program multiple times.
• Share data between programs.
We need to know:
how to "connect" file to program
how to tell the program to read data
how to tell the program to write data
error checking and handling EOF
• A file can be opened in two ways:
1.Using constructor function
2.Using member function
Opening file using constructor
• File name is used to initialize the file stream object.
• Steps:-
1.Create a file stream object to manage stream with
appropriate class.
2.Initialize the file object with desired name.
Contd……..
• ofstream outfile(“results”); //it opens a file name
“result” for output.
• ifstream infile(“data”);
outfile<<“total”;
outfile.close();
infile>> string;
Opening files using member function
open()
• This function is used to open multiple files that uses
same stream object.
• Syntax:-
file_stream class stream_object;
stream_object.open(“filename”);
Example
ofstream outfile;
outfile.open(“Data”);
…………………..
…………………..
outfile.close();
outfile.open(“Data2”);
…………………….
…………………..
outfile.close();
Open(): File Modes
• We can have two arguments in open(), to specify
the file-mode.
stream-object.open(“filename”,mode);
the second argument specifies the purpose for which
file is opened.
Stream state member functions
• In C++, file stream classes inherit a stream state
member from the ios class, which gives out the
information regarding the status of the stream.
For e.g.:
eof() –used to check the end of file charactereof() –used to check the end of file character
fail()- used to check the status of file atfail()- used to check the status of file at
opening for I/Oopening for I/O
bad()- used to check whether invalid filebad()- used to check whether invalid file
operations or unrecoverable error .operations or unrecoverable error .
good()- used to check whether the previous filegood()- used to check whether the previous file
operation has been successfuloperation has been successful
EXAMPLES
• EOF()
Void main()
{
Ifstream infile;
Infile.open(“text”);
While(!infile.eof())
{
------
-----
} }
• FAIL()
Main()
{
Ifstream infile;
Infile.open(“text”);
While(!infile.fail())
{
Cout<<“cudn’t open a file”;
} }
Reading and Writing in Files
• Reading and Writing a character from a file can be
done by
• Get()
• Put()
• Get():- This is used to read an alphanumeric
character from a specified file.
• Put():- This is used to write an alphanumeric
character to specified file.
Reading and writing by insertion and
extraction
Stream Insertion Operators
• Are defined in the ostream class
• The operator “<<” is called the inserter
Stream Extraction Operators
• Are defined in the istream class and are used to
receive data from the input device
• The operator “>>”, called the extractor.
By Read () and Write()
• Read and write is used when we are dealing with
classes.
• Syntax:-
Read((char*)&obj sizeof(obj));
Write((char*)&obj,sizeof(obj));
EXAMPLE
MEMBER FUNCTIONS
• ios::app = append at end of file
• Ios::ate = go to end of file on opening instead of
beginning.
• Ios::binary=binary file
• Ios::in = open file for reading only
• Ios::out = open file for writing only
• Ios:: trunc = delete the content of file if it exists
• Ios::nocreate = open fails if file doesn’t exist.
EXAMPLE
1) ofstream fileout;
fileout.open(“hello”,ios::app);
2)fileout.open(“hello”, ios::in||ios::out);

More Related Content

What's hot (20)

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++
 
Files in c++
Files in c++Files in c++
Files in c++
 
Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)Pf cs102 programming-8 [file handling] (1)
Pf cs102 programming-8 [file handling] (1)
 
Files in c++
Files in c++Files in c++
Files in c++
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Filehandlinging cp2
Filehandlinging cp2Filehandlinging cp2
Filehandlinging cp2
 
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
 
Java file
Java fileJava file
Java file
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
File handling
File handlingFile handling
File handling
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Java File I/O
Java File I/OJava File I/O
Java File I/O
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
File Pointers
File PointersFile Pointers
File Pointers
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 

Similar to File Handling in C

Similar to File Handling in C (20)

Files and streams
Files and streamsFiles and streams
Files and streams
 
File handling
File handlingFile handling
File handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
ch09.ppt
ch09.pptch09.ppt
ch09.ppt
 
File management in C++
File management in C++File management in C++
File management in C++
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
Linux System Programming - File I/O
Linux System Programming - File I/O Linux System Programming - File I/O
Linux System Programming - File I/O
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
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
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 
File handling.pptx
File handling.pptxFile handling.pptx
File handling.pptx
 
Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O Linux System Programming - Buffered I/O
Linux System Programming - Buffered I/O
 
File management
File managementFile management
File management
 
Intro To C++ - Class #21: Files
Intro To C++ - Class #21: FilesIntro To C++ - Class #21: Files
Intro To C++ - Class #21: Files
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
 

Recently uploaded

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 

Recently uploaded (20)

Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 

File Handling in C

  • 2. Introduction • 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.hfstream.h
  • 3. Contd… • Earlier, only two streams are being used such as cin and cout. • There are some problems with these streams. 1)It is very difficult to handle large volume of data. 2)The data is not permanently stored. Eg:- the calculation of the result of 200 students will be lost when the program terminates.
  • 4. Contd….. • C++ provides a new technique for handling I/O operations through mechanism known as streams. • A stream refers to a flow of data. • Classified in 2 categories: 1. Output stream 2. Input stream In output stream flow of data is from program to the output device. In input stream the flow of data is from input device to a program in main memory.
  • 5. File  Program ( Input stream) - reads Program  File (Output stream) – write
  • 7. Why to use Files: • Convenient way to deal large quantities of data. • Store data permanently (until file is deleted). • Avoid typing data into program multiple times. • Share data between programs. We need to know: how to "connect" file to program how to tell the program to read data how to tell the program to write data error checking and handling EOF
  • 8. • A file can be opened in two ways: 1.Using constructor function 2.Using member function
  • 9. Opening file using constructor • File name is used to initialize the file stream object. • Steps:- 1.Create a file stream object to manage stream with appropriate class. 2.Initialize the file object with desired name.
  • 10. Contd…….. • ofstream outfile(“results”); //it opens a file name “result” for output. • ifstream infile(“data”); outfile<<“total”; outfile.close(); infile>> string;
  • 11. Opening files using member function open() • This function is used to open multiple files that uses same stream object. • Syntax:- file_stream class stream_object; stream_object.open(“filename”);
  • 13. Open(): File Modes • We can have two arguments in open(), to specify the file-mode. stream-object.open(“filename”,mode); the second argument specifies the purpose for which file is opened.
  • 14. Stream state member functions • In C++, file stream classes inherit a stream state member from the ios class, which gives out the information regarding the status of the stream. For e.g.: eof() –used to check the end of file charactereof() –used to check the end of file character fail()- used to check the status of file atfail()- used to check the status of file at opening for I/Oopening for I/O bad()- used to check whether invalid filebad()- used to check whether invalid file operations or unrecoverable error .operations or unrecoverable error . good()- used to check whether the previous filegood()- used to check whether the previous file operation has been successfuloperation has been successful
  • 15. EXAMPLES • EOF() Void main() { Ifstream infile; Infile.open(“text”); While(!infile.eof()) { ------ ----- } }
  • 17. Reading and Writing in Files • Reading and Writing a character from a file can be done by • Get() • Put() • Get():- This is used to read an alphanumeric character from a specified file. • Put():- This is used to write an alphanumeric character to specified file.
  • 18. Reading and writing by insertion and extraction Stream Insertion Operators • Are defined in the ostream class • The operator “<<” is called the inserter Stream Extraction Operators • Are defined in the istream class and are used to receive data from the input device • The operator “>>”, called the extractor.
  • 19. By Read () and Write() • Read and write is used when we are dealing with classes. • Syntax:- Read((char*)&obj sizeof(obj)); Write((char*)&obj,sizeof(obj)); EXAMPLE
  • 20. MEMBER FUNCTIONS • ios::app = append at end of file • Ios::ate = go to end of file on opening instead of beginning. • Ios::binary=binary file • Ios::in = open file for reading only • Ios::out = open file for writing only • Ios:: trunc = delete the content of file if it exists • Ios::nocreate = open fails if file doesn’t exist.