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
TXT
Android APP-toggle case
PPTX
Namespace1
PPTX
Data File Handiling File POINTERS IN C++
PDF
Cpp lab 13_pres
PPTX
Exported resources design patterns
PDF
Apache avro and overview hadoop tools
PPTX
Commit2015 kharchenko - python generators - ext
C++ prgms io file unit 7
Android APP-toggle case
Namespace1
Data File Handiling File POINTERS IN C++
Cpp lab 13_pres
Exported resources design patterns
Apache avro and overview hadoop tools
Commit2015 kharchenko - python generators - ext

What's hot (20)

PPT
Oop lecture9 11
PPT
eZ Publish cluster unleashed revisited
PPT
cpp-file-handling
DOC
Inheritance
PPTX
Files in php
DOCX
java copy file program
PDF
Php file handling in Hindi
PDF
PuppetDB, Puppet Explorer and puppetdbquery
PPT
PHP - Introduction to File Handling with PHP
PPTX
2015 555 kharchenko_ppt
PPTX
Files in c++
PDF
Gsummit apis-2012
PDF
Impala: A Modern, Open-Source SQL Engine for Hadoop
PPTX
Get docs from sp doc library
PDF
PHP file handling
PPTX
Php File Operations
PPT
File handling in c
PPTX
Database Homework Help
PPTX
File management
PPTX
Database Homework Help
Oop lecture9 11
eZ Publish cluster unleashed revisited
cpp-file-handling
Inheritance
Files in php
java copy file program
Php file handling in Hindi
PuppetDB, Puppet Explorer and puppetdbquery
PHP - Introduction to File Handling with PHP
2015 555 kharchenko_ppt
Files in c++
Gsummit apis-2012
Impala: A Modern, Open-Source SQL Engine for Hadoop
Get docs from sp doc library
PHP file handling
Php File Operations
File handling in c
Database Homework Help
File management
Database Homework Help
Ad

Similar to working with files (20)

PPTX
Basics of file handling
PPTX
basics of file handling
PDF
Files in C++.pdf is the notes of cpp for reference
PDF
Filesinc 130512002619-phpapp01
PPSX
Files in c++
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PPT
File handling in C++
PPTX
Introduction to files management systems
PPT
File handling in_c
PPT
data file handling
PPT
File in cpp 2016
PPT
file_handling_in_c.ppt......................................
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPTX
Stream classes in C++
PDF
Files and streams
PDF
File Handling.pdffile handling ppt final
PPT
Cpp file-handling
PPT
7 Data File Handling
PDF
DOC-20241121-WA0004bwushshusjssjuwh..pdf
PPT
Unit5
Basics of file handling
basics of file handling
Files in C++.pdf is the notes of cpp for reference
Filesinc 130512002619-phpapp01
Files in c++
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
File handling in C++
Introduction to files management systems
File handling in_c
data file handling
File in cpp 2016
file_handling_in_c.ppt......................................
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
Stream classes in C++
Files and streams
File Handling.pdffile handling ppt final
Cpp file-handling
7 Data File Handling
DOC-20241121-WA0004bwushshusjssjuwh..pdf
Unit5
Ad

More from SangeethaSasi1 (20)

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

Recently uploaded (20)

PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Basic Mud Logging Guide for educational purpose
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
human mycosis Human fungal infections are called human mycosis..pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
O5-L3 Freight Transport Ops (International) V1.pdf
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Types and Its function , kingdom of life
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial diseases, their pathogenesis and prophylaxis
Basic Mud Logging Guide for educational purpose
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
102 student loan defaulters named and shamed – Is someone you know on the list?
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.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