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

PDF
C++ prgms io file unit 7
Ananda Kumar HN
 
TXT
Android APP-toggle case
Liang (Leon) Zhou
 
PPTX
Namespace1
zindadili
 
PPTX
Data File Handiling File POINTERS IN C++
subham sahu
 
PDF
Cpp lab 13_pres
Youth For Peace
 
PPTX
Exported resources design patterns
Yevgeny Trachtinov
 
PDF
Apache avro and overview hadoop tools
alireza alikhani
 
PPTX
Commit2015 kharchenko - python generators - ext
Maxym Kharchenko
 
C++ prgms io file unit 7
Ananda Kumar HN
 
Android APP-toggle case
Liang (Leon) Zhou
 
Namespace1
zindadili
 
Data File Handiling File POINTERS IN C++
subham sahu
 
Cpp lab 13_pres
Youth For Peace
 
Exported resources design patterns
Yevgeny Trachtinov
 
Apache avro and overview hadoop tools
alireza alikhani
 
Commit2015 kharchenko - python generators - ext
Maxym Kharchenko
 

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
 
Ad

Similar to working with files (20)

PPTX
Filesin c++
HalaiHansaika
 
PPTX
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
PPTX
Working with files in c++. file handling
tfluid16
 
PDF
Files and streams
Pranali Chaudhari
 
PDF
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
PPT
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
PPT
Data file handling
Saurabh Patel
 
PDF
Data file handling
Prof. Dr. K. Adisesha
 
PPTX
Cs1123 10 file operations
TAlha MAlik
 
PPTX
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
PDF
C++ Files and Streams
Ahmed Farag
 
PPSX
Files in c++
Selvin Josy Bai Somu
 
PDF
Filesinc 130512002619-phpapp01
Rex Joe
 
PPTX
Streams and Files
Munazza-Mah-Jabeen
 
PPT
File handling
Nilesh Dalvi
 
PDF
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
PPTX
Stream classes in C++
Shyam Gupta
 
PPT
csc1201_lecture13.ppt
HEMAHEMS5
 
PDF
File Handling.pdffile handling ppt final
e13225064
 
PPTX
file handling final3333.pptx
radhushri
 
Filesin c++
HalaiHansaika
 
file.pptx 43dcsddsafgdewdvvbghghsdwweffr
abdelhamidatef1
 
Working with files in c++. file handling
tfluid16
 
Files and streams
Pranali Chaudhari
 
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Venugopalavarma Raja
 
Data file handling
Saurabh Patel
 
Data file handling
Prof. Dr. K. Adisesha
 
Cs1123 10 file operations
TAlha MAlik
 
13 file handling in C++.pptx oops object oriented programming
archana22486y
 
C++ Files and Streams
Ahmed Farag
 
Files in c++
Selvin Josy Bai Somu
 
Filesinc 130512002619-phpapp01
Rex Joe
 
Streams and Files
Munazza-Mah-Jabeen
 
File handling
Nilesh Dalvi
 
DOC-20241121-WA0004bwushshusjssjuwh..pdf
TanishaWaichal
 
Stream classes in C++
Shyam Gupta
 
csc1201_lecture13.ppt
HEMAHEMS5
 
File Handling.pdffile handling ppt final
e13225064
 
file handling final3333.pptx
radhushri
 
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
 

Recently uploaded (20)

PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Virus sequence retrieval from NCBI database
yamunaK13
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 

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