SlideShare ist ein Scribd-Unternehmen logo
1 von 18
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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Oop lecture9 11
Oop lecture9 11Oop lecture9 11
Oop lecture9 11
 
eZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisitedeZ Publish cluster unleashed revisited
eZ Publish cluster unleashed revisited
 
cpp-file-handling
cpp-file-handlingcpp-file-handling
cpp-file-handling
 
Inheritance
InheritanceInheritance
Inheritance
 
Files in php
Files in phpFiles in php
Files in php
 
java copy file program
java copy file programjava copy file program
java copy file program
 
Php file handling in Hindi
Php file handling in Hindi Php file handling in Hindi
Php file handling in Hindi
 
PuppetDB, Puppet Explorer and puppetdbquery
PuppetDB, Puppet Explorer and puppetdbqueryPuppetDB, Puppet Explorer and puppetdbquery
PuppetDB, Puppet Explorer and puppetdbquery
 
PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
 
2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
Files in c++
Files in c++Files in c++
Files in c++
 
Gsummit apis-2012
Gsummit apis-2012Gsummit apis-2012
Gsummit apis-2012
 
Impala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for HadoopImpala: A Modern, Open-Source SQL Engine for Hadoop
Impala: A Modern, Open-Source SQL Engine for Hadoop
 
Get docs from sp doc library
Get docs from sp doc libraryGet docs from sp doc library
Get docs from sp doc library
 
PHP file handling
PHP file handling PHP file handling
PHP file handling
 
Php File Operations
Php File OperationsPhp File Operations
Php File Operations
 
File handling in c
File handling in c File handling in c
File handling in c
 
Database Homework Help
Database Homework HelpDatabase Homework Help
Database Homework Help
 
File management
File managementFile management
File management
 
Database Homework Help
Database Homework HelpDatabase Homework Help
Database Homework Help
 

Ähnlich wie working with files

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
 
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
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
TAlha MAlik
 

Ähnlich wie working with files (20)

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
 
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
 
Files in c++
Files in c++Files in c++
Files in c++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
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
 
data file handling
data file handlingdata file handling
data file handling
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
Unit5
Unit5Unit5
Unit5
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
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
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 

Mehr von SangeethaSasi1 (20)

L4 multiplexing &amp; multiple access 16
L4 multiplexing &amp; multiple access 16L4 multiplexing &amp; multiple access 16
L4 multiplexing &amp; multiple access 16
 
Image processing using matlab
Image processing using matlab Image processing using matlab
Image processing using matlab
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Mc ppt
Mc pptMc ppt
Mc ppt
 
Dip pppt
Dip ppptDip pppt
Dip pppt
 
Web techh
Web techhWeb techh
Web techh
 
Web tech
Web techWeb tech
Web tech
 
Vani wt
Vani wtVani wt
Vani wt
 
Vani dbms
Vani dbmsVani dbms
Vani dbms
 
Hema wt (1)
Hema wt (1)Hema wt (1)
Hema wt (1)
 
Hema rdbms
Hema rdbmsHema rdbms
Hema rdbms
 
Web tech
Web techWeb tech
Web tech
 
Web tech
Web techWeb tech
Web tech
 
Dbms
DbmsDbms
Dbms
 
Vani
VaniVani
Vani
 
Hema se
Hema seHema se
Hema se
 
Software
SoftwareSoftware
Software
 
Operating system
Operating systemOperating system
Operating system
 
Dataminng
DataminngDataminng
Dataminng
 
System calls
System callsSystem calls
System calls
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

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