SlideShare a Scribd company logo
WORKING WITH FILES
N.SURATHAVANI.Msc(Information
Technology)
NADAR SARAWATHI COLLEGE OF ARTS
AND SCIENCETHENI.
The file is the many data are stored
in the particular place in the disk.
The many methods are available for
storing in the file and retrieving
from the file.
Input stream
The input stream is use to supplies data
in the program.
Output stream
The output stream is use to receives
data from the file.
streams
File input and output streams
Read Data
Data input
Write Data
Data output
Disk files Program
File stream classes
Class Contents
filebuf
To set the buffers to read and write.It contain
close() and open() as members.
fstreambase
Provides operations common to the file streams.
Serves as a base for fstream,ifstream and
ofstream class.
ifstream
Provides input operations. Inherites the functions
get(),getline(),read(),seekg() and tellg()
functions from istream.
ofstream
Provides output operations. Inherits put(),
seekp(),tellp() and write() functions from
ostream.
fstreams
Provides input and output operations. Inherits all
functions from istream and ostream classes
through iostream.
 Suitable name for the file
Data type and structure
Purpose
Opening method
OPENING AND CLOSING A FILE
FILE CAN BE OPENED IN TWO WAYS:
FILE
CONSTRUCTOR
FUNCTION
MEMBER
FUNCTION OPEN()
A filename is used to initialize the file stream
object.
Create a file stream object to manage the
stream using the appropriate class.
The class ofstream is used to create the
output stream and the class ifstream to
create the input stream.
Initialize the file object with the desired file
name.
OPENING FILE USING CONSTRUCTOR
Disk
Output stream
outfile
Input stream
infile
TWO FILE STREAMS WORKING ON
SEPERATE FILES
program
Result
file
Data
file
#include<iostream.h>
#include<fstream.h>
int main()
{
ofstream outf(“ITEM”);
cout << “enter item name:”;
char name[30];
cin >> name;
outf << name<<“n”;
cout << “enter item cost:”;
WORKING WITH SINGLE FILE
float cost;
cin >> cost;
outf << cost << “n”;
outf.close();
ifstream inf(“ITEM”);
inf >> name;
Inf >> cost;
Cout << “n”;
Cout << “item name:” << name << “n”;
Cout << “item cost:” << cost << “n”;
Inf.close();
Return 0;
}
The function open() can be used to open
multiple files that use the same stream object.
SYNTAX:
OPENING FILE USING OPEN()
file-stream-class stream-object;
stream-object.open(“filename”);
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main()
{
const int SIZE=80;
char line[SIZE];
ifstream fin1, fin2;
fin1.open(“country”);
READING FROM TWO FILES
fin2.open(“capital”);
for (int i=1; i<=10; i++)
{
if (fin1.eof() != 0)
{
cout << “exit from country n”;
exit(1);
}
fin1.getline(line, SIZE);
cout << “capital of” << line;
if (fin2.eof () !=0)
{
cout << “exit from capital n”;
exit (1);
}
fin2.getline (line,SIZE);
cout << line << “n”;
}
return 0;
}
The ifstream and ofstream constructors and the
function open() to create new files as well as to open
the existing files.
SYNTAX:
MORE ABOUT OPEN(): FILE MODES
stream-object.open(“filename”,mode);
The file mode parameter specifies the
purpose for which the file is opened.
The file mode parameter can take one of such
constants defined in the class ios.
ios::in for ifstream functions meaning open for
reading only.
ios::out for ofstream function meaning open for
writing only.
FILE MODE PARAMETERS
PARAMETER MEANING
ios::app Append to end-of-file
ios::ate Go to end-of-file on opening
ios::binary Binary file
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already exists
ios::out Open file for writing only
ios::trunc Delete the contents of the file if it exists

More Related Content

What's hot (20)

PPT
Oop lecture9 11
Shahriar Robbani
 
PPT
eZ Publish cluster unleashed revisited
Bertrand Dunogier
 
PPT
cpp-file-handling
zohaib ali ali
 
DOC
Inheritance
آصف الصيفي
 
PPTX
Files in php
sana mateen
 
DOCX
java copy file program
Glen Pais
 
PDF
Php file handling in Hindi
Vipin sharma
 
PDF
PuppetDB, Puppet Explorer and puppetdbquery
Puppet
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPTX
2015 555 kharchenko_ppt
Maxym Kharchenko
 
PPTX
Files in c++
NivethaJeyaraman
 
PDF
Gsummit apis-2012
Gluster.org
 
PDF
Impala: A Modern, Open-Source SQL Engine for Hadoop
All Things Open
 
PPTX
Get docs from sp doc library
Sudip Sengupta
 
PDF
PHP file handling
wahidullah mudaser
 
PPTX
Php File Operations
Jamshid Hashimi
 
PPT
File handling in c
Vikash Dhal
 
PPTX
Database Homework Help
Database Homework Help
 
PPTX
File management
Mohammed Sikander
 
PPTX
Database Homework Help
Database Homework Help
 
Oop lecture9 11
Shahriar Robbani
 
eZ Publish cluster unleashed revisited
Bertrand Dunogier
 
cpp-file-handling
zohaib ali ali
 
Inheritance
آصف الصيفي
 
Files in php
sana mateen
 
java copy file program
Glen Pais
 
Php file handling in Hindi
Vipin sharma
 
PuppetDB, Puppet Explorer and puppetdbquery
Puppet
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
2015 555 kharchenko_ppt
Maxym Kharchenko
 
Files in c++
NivethaJeyaraman
 
Gsummit apis-2012
Gluster.org
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
All Things Open
 
Get docs from sp doc library
Sudip Sengupta
 
PHP file handling
wahidullah mudaser
 
Php File Operations
Jamshid Hashimi
 
File handling in c
Vikash Dhal
 
Database Homework Help
Database Homework Help
 
File management
Mohammed Sikander
 
Database Homework Help
Database Homework Help
 

Similar to working with files (20)

PPTX
Basics of file handling
pinkpreet_kaur
 
PPTX
basics of file handling
pinkpreet_kaur
 
PDF
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PDF
Filesinc 130512002619-phpapp01
Rex Joe
 
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
PPT
File handling in C++
Hitesh Kumar
 
PPTX
Introduction to files management systems
araba8
 
PPT
File handling in_c
sanya6900
 
PPT
data file handling
krishna partiwala
 
PPT
File in cpp 2016
Dr .Ahmed Tawwab
 
PPT
file_handling_in_c.ppt......................................
nadoj47203
 
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
PPTX
Stream classes in C++
Shyam Gupta
 
PDF
Files and streams
Pranali Chaudhari
 
PDF
File Handling.pdffile handling ppt final
e13225064
 
PPT
Cpp file-handling
JiaahRajpout123
 
PPT
7 Data File Handling
Praveen M Jigajinni
 
PDF
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
PPT
Unit5
mrecedu
 
Basics of file handling
pinkpreet_kaur
 
basics of file handling
pinkpreet_kaur
 
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
Files in c++
Selvin Josy Bai Somu
 
Filesinc 130512002619-phpapp01
Rex Joe
 
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
AzanMehdi
 
File handling in C++
Hitesh Kumar
 
Introduction to files management systems
araba8
 
File handling in_c
sanya6900
 
data file handling
krishna partiwala
 
File in cpp 2016
Dr .Ahmed Tawwab
 
file_handling_in_c.ppt......................................
nadoj47203
 
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
SanskritiGupta39
 
Stream classes in C++
Shyam Gupta
 
Files and streams
Pranali Chaudhari
 
File Handling.pdffile handling ppt final
e13225064
 
Cpp file-handling
JiaahRajpout123
 
7 Data File Handling
Praveen M Jigajinni
 
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
Unit5
mrecedu
 
Ad

More from SangeethaSasi1 (20)

PPT
L4 multiplexing &amp; multiple access 16
SangeethaSasi1
 
PPT
Image processing using matlab
SangeethaSasi1
 
PPTX
Mc ppt
SangeethaSasi1
 
PPTX
Mc ppt
SangeethaSasi1
 
PPTX
Dip pppt
SangeethaSasi1
 
PPTX
Web techh
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Vani wt
SangeethaSasi1
 
PPTX
Vani dbms
SangeethaSasi1
 
PPTX
Hema wt (1)
SangeethaSasi1
 
PPTX
Hema rdbms
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Web tech
SangeethaSasi1
 
PPTX
Dbms
SangeethaSasi1
 
PPTX
Vani
SangeethaSasi1
 
PPTX
Hema se
SangeethaSasi1
 
PPTX
Software
SangeethaSasi1
 
PPTX
Operating system
SangeethaSasi1
 
PPTX
Dataminng
SangeethaSasi1
 
PPTX
System calls
SangeethaSasi1
 
L4 multiplexing &amp; multiple access 16
SangeethaSasi1
 
Image processing using matlab
SangeethaSasi1
 
Dip pppt
SangeethaSasi1
 
Web techh
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Vani dbms
SangeethaSasi1
 
Hema wt (1)
SangeethaSasi1
 
Hema rdbms
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Web tech
SangeethaSasi1
 
Software
SangeethaSasi1
 
Operating system
SangeethaSasi1
 
Dataminng
SangeethaSasi1
 
System calls
SangeethaSasi1
 
Ad

Recently uploaded (20)

PPTX
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PDF
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
John Keats introduction and list of his important works
vatsalacpr
 
PPTX
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PPTX
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPT
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
PPTX
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PDF
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 
Cybersecurity: How to Protect your Digital World from Hackers
vaidikpanda4
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
TOP 10 AI TOOLS YOU MUST LEARN TO SURVIVE IN 2025 AND ABOVE
digilearnings.com
 
Basics and rules of probability with real-life uses
ravatkaran694
 
John Keats introduction and list of his important works
vatsalacpr
 
I INCLUDED THIS TOPIC IS INTELLIGENCE DEFINITION, MEANING, INDIVIDUAL DIFFERE...
parmarjuli1412
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
My Thoughts On Q&A- A Novel By Vikas Swarup
Niharika
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
The Future of Artificial Intelligence Opportunities and Risks Ahead
vaghelajayendra784
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Virus sequence retrieval from NCBI database
yamunaK13
 
DRUGS USED IN THERAPY OF SHOCK, Shock Therapy, Treatment or management of shock
Rajshri Ghogare
 
Electrophysiology_of_Heart. Electrophysiology studies in Cardiovascular syste...
Rajshri Ghogare
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
EXCRETION-STRUCTURE OF NEPHRON,URINE FORMATION
raviralanaresh2
 

working with files

  • 1. WORKING WITH FILES N.SURATHAVANI.Msc(Information Technology) NADAR SARAWATHI COLLEGE OF ARTS AND SCIENCETHENI.
  • 2. The file is the many data are stored in the particular place in the disk. The many methods are available for storing in the file and retrieving from the file.
  • 3. Input stream The input stream is use to supplies data in the program. Output stream The output stream is use to receives data from the file. streams
  • 4. File input and output streams Read Data Data input Write Data Data output Disk files Program
  • 5. File stream classes Class Contents filebuf To set the buffers to read and write.It contain close() and open() as members. fstreambase Provides operations common to the file streams. Serves as a base for fstream,ifstream and ofstream class. ifstream Provides input operations. Inherites the functions get(),getline(),read(),seekg() and tellg() functions from istream. ofstream Provides output operations. Inherits put(), seekp(),tellp() and write() functions from ostream. fstreams Provides input and output operations. Inherits all functions from istream and ostream classes through iostream.
  • 6.  Suitable name for the file Data type and structure Purpose Opening method OPENING AND CLOSING A FILE
  • 7. FILE CAN BE OPENED IN TWO WAYS: FILE CONSTRUCTOR FUNCTION MEMBER FUNCTION OPEN()
  • 8. A filename is used to initialize the file stream object. Create a file stream object to manage the stream using the appropriate class. The class ofstream is used to create the output stream and the class ifstream to create the input stream. Initialize the file object with the desired file name. OPENING FILE USING CONSTRUCTOR
  • 9. Disk Output stream outfile Input stream infile TWO FILE STREAMS WORKING ON SEPERATE FILES program Result file Data file
  • 10. #include<iostream.h> #include<fstream.h> int main() { ofstream outf(“ITEM”); cout << “enter item name:”; char name[30]; cin >> name; outf << name<<“n”; cout << “enter item cost:”; WORKING WITH SINGLE FILE
  • 11. float cost; cin >> cost; outf << cost << “n”; outf.close(); ifstream inf(“ITEM”); inf >> name; Inf >> cost; Cout << “n”; Cout << “item name:” << name << “n”; Cout << “item cost:” << cost << “n”; Inf.close(); Return 0; }
  • 12. The function open() can be used to open multiple files that use the same stream object. SYNTAX: OPENING FILE USING OPEN() file-stream-class stream-object; stream-object.open(“filename”);
  • 13. #include <iostream.h> #include <fstream.h> #include <stdlib.h> int main() { const int SIZE=80; char line[SIZE]; ifstream fin1, fin2; fin1.open(“country”); READING FROM TWO FILES
  • 14. fin2.open(“capital”); for (int i=1; i<=10; i++) { if (fin1.eof() != 0) { cout << “exit from country n”; exit(1); } fin1.getline(line, SIZE); cout << “capital of” << line; if (fin2.eof () !=0)
  • 15. { cout << “exit from capital n”; exit (1); } fin2.getline (line,SIZE); cout << line << “n”; } return 0; }
  • 16. The ifstream and ofstream constructors and the function open() to create new files as well as to open the existing files. SYNTAX: MORE ABOUT OPEN(): FILE MODES stream-object.open(“filename”,mode);
  • 17. The file mode parameter specifies the purpose for which the file is opened. The file mode parameter can take one of such constants defined in the class ios. ios::in for ifstream functions meaning open for reading only. ios::out for ofstream function meaning open for writing only.
  • 18. FILE MODE PARAMETERS PARAMETER MEANING ios::app Append to end-of-file ios::ate Go to end-of-file on opening ios::binary Binary file ios::in Open file for reading only ios::nocreate Open fails if the file does not exist ios::noreplace Open fails if the file already exists ios::out Open file for writing only ios::trunc Delete the contents of the file if it exists