SlideShare a Scribd company logo
1 of 19
FILEHANDLING IN C++
SOBIN JOSE
Sobin.jose@outlook.com
Sobin jose
Sobin jose
Typing speed:21wpm
Disclaimer: This presentation is prepared by trainees of
baabtra.com as a part of mentoring program. This is not
official document of baabtra.com – Mentoring Partner
What is a File?
• A file is a collection on information, usually
stored on a computer’s disk. Information
can be saved to files and then later reused.
Classes for file stream operation
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
The Process of Using a File
• Using a file in a program is a simple three-
step process
– The file must be opened. If the file does not
yet exits, opening it means creating it.
– Information is then saved to the file, read from
the file, or both.
– When the program is finished using the file,
the file must be closed.
Opening a File
• Before data can be written to or read from
a file, the file must be opened.
ifstream inputFile;
inputFile.open(“customer.dat”);
// This program demonstrates the declaration of an fstream
// object and the opening of a file.
#include <iostream.h>
#include <fstream.h>
void main(void)
{
fstream dataFile; // Declare file stream object
char fileName[81];
cout << "Enter the name of a file you wish to openn";
cout << "or create: ";
cin.getline(fileName, 81);
dataFile.open(fileName, ios::out);
cout << "The file " << fileName << " was opened.n";
}
File Modes
Name Description
ios::in Open file to read
ios::out Open file to write
ios::app All the date you write, is put at the end of the file.
It calls ios::out
ios::ate All the date you write, is put at the end of the file.
It does not call ios::out
ios::trunc Deletes all previous content in the file. (empties
the file)
ios::nocreate If the file does not exist, opening it with the open()
function gets impossible.
ios::noreplace If the file exists, trying to open it with the open()
function, returns an error.
ios::binary Opens the file in binary mode.
#include<fstream>
int main()
{
ofstream fout;
fout.open("out.txt");
char str[300]="Time is a great teacher
but unfortunately it kills all its pupils.
Berlioz";
fout<<str;
fout.close();
return 0;
}
INPUT AND OUTPUT OPERATION
put() and get() function
the function put() writes a single character to the associated stream.
Similarly, the function get() reads a single character form the
associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));
File Pointers And Their Manipulation
•ifstream, like istream, has a pointer known as the get
pointer that points to the element to be read in the next
input operation.
•ofstream, like ostream, has a pointer known as the put
pointer that points to the location where the next element
has to be written.
•Finally, fstream, inherits both, the get and the put pointers,
from iostream (which is itself derived from both istream
and ostream).
seekg()
moves get pointer(input) to a
specified location
seekp()
moves put pointer (output) to
a specified location
tellg()
gives the current position of
the get pointer
tellp()
gives the current position of
the put pointer
These internal stream pointers that point to the reading or writing locations within a
stream can be manipulated using the following member functions:
The other prototype for these functions is:
seekg(offset, refposition );
seekp(offset, refposition );
•The parameter offset represents the number of
bytes the file pointer is to be moved from the
location specified by the parameter refposition.
The refposition takes one of the following three
constants defined in the ios class.
•ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file
•example:
file.seekg(-10, ios::cur);
Binary File I/O Examples
//Example 1: Using get() and put()
#include <iostream>
#include <fstream>
void main()
{
fstream File("test_file",ios::out | ios::in | ios::binary);
char ch;
ch='o';
File.put(ch); //put the content of ch to the file
File.seekg(ios::beg); //go to the beginning of the file
File.get(ch); //read one character
cout << ch << endl; //display it
File.close();
}
File I/O Example: Reading
#include <iostream>
#include <fstream>
#include <string>
int main(void)
{
ifstream openFile(“data.txt"); //open a text file data.txt
string line;
if(openFile.is_open()){ //
while(!openFile.eof()){
getline(openFile,line);//read a line from data.txt and put it in a string
cout << line;
}
else{
cout<<“File does not exist!”<<endl;
exit(1);}
}
openFile.close();
return 0;
}
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many contents online?
Do you want to learn things quickly?
Tired of spending huge amount of money to become a Software professional?
Do an online course
@ baabtra.com
We put industry standards to practice. Our structured, activity based courses are so designed
to make a quick, good software professional out of anybody who holds a passion for coding.
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ slideshare.net/BaabtraMentoringPartner
Connect to us @ in.linkedin.com/in/baabtra
Give a feedback @ massbaab.com/baabtra
Thanks in advance
www.baabtra.com | www.massbaab.com |www.baabte.com
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Cafit Square,
Hilite Business Park,
Near Pantheerankavu,
Kozhikode
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
Contact Us

More Related Content

What's hot (20)

7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
17 files and streams
17 files and streams17 files and streams
17 files and streams
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
Data file handling in python reading & writing methods
Data file handling in python reading & writing methodsData file handling in python reading & writing methods
Data file handling in python reading & writing methods
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
File Pointers
File PointersFile Pointers
File Pointers
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Data file handling
Data file handlingData file handling
Data file handling
 
Filehadnling
FilehadnlingFilehadnling
Filehadnling
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
file handling, dynamic memory allocation
file handling, dynamic memory allocationfile handling, dynamic memory allocation
file handling, dynamic memory allocation
 
basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
File Handling and Command Line Arguments in C
File Handling and Command Line Arguments in CFile Handling and Command Line Arguments in C
File Handling and Command Line Arguments in C
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Cpp file-handling
Cpp file-handlingCpp file-handling
Cpp file-handling
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Python file handling
Python file handlingPython file handling
Python file handling
 

Viewers also liked

Viewers also liked (17)

Oop concepts
Oop conceptsOop concepts
Oop concepts
 
Master page
Master pageMaster page
Master page
 
Cpu execution
Cpu executionCpu execution
Cpu execution
 
Transactions
TransactionsTransactions
Transactions
 
E paper
E paperE paper
E paper
 
Jquery
JqueryJquery
Jquery
 
Li fi
Li fiLi fi
Li fi
 
Fitness band
Fitness bandFitness band
Fitness band
 
Scope of variables
Scope of variablesScope of variables
Scope of variables
 
First android app for workshop using android studio
First android app for workshop using android studio First android app for workshop using android studio
First android app for workshop using android studio
 
Hadoop 2.4 installing on ubuntu 14.04
Hadoop 2.4 installing on ubuntu 14.04Hadoop 2.4 installing on ubuntu 14.04
Hadoop 2.4 installing on ubuntu 14.04
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
Oops
OopsOops
Oops
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Google loon - baabtra.com
Google loon - baabtra.comGoogle loon - baabtra.com
Google loon - baabtra.com
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
How to create a chat application on Android platform?
How to create a chat application on Android platform? How to create a chat application on Android platform?
How to create a chat application on Android platform?
 

Similar to C++ File Handling Guide: Learn File Input/Output Streams

Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Rex Joe
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operationsTAlha MAlik
 
File management in C++
File management in C++File management in C++
File management in C++apoorvaverma33
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
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 referenceanuvayalil5525
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSVenugopalavarma Raja
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.pptHEMAHEMS5
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5YOGESH SINGH
 

Similar to C++ File Handling Guide: Learn File Input/Output Streams (20)

Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.2 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.2 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
 pointer, structure ,union and intro to file handling pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
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
 
Cs1123 10 file operations
Cs1123 10 file operationsCs1123 10 file operations
Cs1123 10 file operations
 
File management in C++
File management in C++File management in C++
File management in C++
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
File operations
File operationsFile operations
File operations
 
File handling
File handlingFile handling
File handling
 
Filehandling
FilehandlingFilehandling
Filehandling
 
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 and streams
Files and streamsFiles and streams
Files and streams
 
data file handling
data file handlingdata file handling
data file handling
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
csc1201_lecture13.ppt
csc1201_lecture13.pptcsc1201_lecture13.ppt
csc1201_lecture13.ppt
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
File handling
File handlingFile handling
File handling
 

More from baabtra.com - No. 1 supplier of quality freshers

More from baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 
Cell phone jammer
Cell phone jammerCell phone jammer
Cell phone jammer
 

C++ File Handling Guide: Learn File Input/Output Streams

  • 1.
  • 2. FILEHANDLING IN C++ SOBIN JOSE Sobin.jose@outlook.com Sobin jose Sobin jose Typing speed:21wpm
  • 3. Disclaimer: This presentation is prepared by trainees of baabtra.com as a part of mentoring program. This is not official document of baabtra.com – Mentoring Partner
  • 4. What is a File? • A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
  • 5. Classes for file stream operation ofstream: Stream class to write on files ifstream: Stream class to read from files fstream: Stream class to both read and write from/to files.
  • 6. The Process of Using a File • Using a file in a program is a simple three- step process – The file must be opened. If the file does not yet exits, opening it means creating it. – Information is then saved to the file, read from the file, or both. – When the program is finished using the file, the file must be closed.
  • 7. Opening a File • Before data can be written to or read from a file, the file must be opened. ifstream inputFile; inputFile.open(“customer.dat”);
  • 8. // This program demonstrates the declaration of an fstream // object and the opening of a file. #include <iostream.h> #include <fstream.h> void main(void) { fstream dataFile; // Declare file stream object char fileName[81]; cout << "Enter the name of a file you wish to openn"; cout << "or create: "; cin.getline(fileName, 81); dataFile.open(fileName, ios::out); cout << "The file " << fileName << " was opened.n"; }
  • 9. File Modes Name Description ios::in Open file to read ios::out Open file to write ios::app All the date you write, is put at the end of the file. It calls ios::out ios::ate All the date you write, is put at the end of the file. It does not call ios::out ios::trunc Deletes all previous content in the file. (empties the file) ios::nocreate If the file does not exist, opening it with the open() function gets impossible. ios::noreplace If the file exists, trying to open it with the open() function, returns an error. ios::binary Opens the file in binary mode.
  • 10. #include<fstream> int main() { ofstream fout; fout.open("out.txt"); char str[300]="Time is a great teacher but unfortunately it kills all its pupils. Berlioz"; fout<<str; fout.close(); return 0; }
  • 11. INPUT AND OUTPUT OPERATION put() and get() function the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream. example : file.get(ch); file.put(ch); write() and read() function write() and read() functions write and read blocks of binary data. example: file.read((char *)&obj, sizeof(obj)); file.write((char *)&obj, sizeof(obj));
  • 12. File Pointers And Their Manipulation •ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation. •ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written. •Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream).
  • 13. seekg() moves get pointer(input) to a specified location seekp() moves put pointer (output) to a specified location tellg() gives the current position of the get pointer tellp() gives the current position of the put pointer These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions: The other prototype for these functions is: seekg(offset, refposition ); seekp(offset, refposition );
  • 14. •The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class. •ios::beg start of the file ios::cur current position of the pointer ios::end end of the file •example: file.seekg(-10, ios::cur);
  • 15. Binary File I/O Examples //Example 1: Using get() and put() #include <iostream> #include <fstream> void main() { fstream File("test_file",ios::out | ios::in | ios::binary); char ch; ch='o'; File.put(ch); //put the content of ch to the file File.seekg(ios::beg); //go to the beginning of the file File.get(ch); //read one character cout << ch << endl; //display it File.close(); }
  • 16. File I/O Example: Reading #include <iostream> #include <fstream> #include <string> int main(void) { ifstream openFile(“data.txt"); //open a text file data.txt string line; if(openFile.is_open()){ // while(!openFile.eof()){ getline(openFile,line);//read a line from data.txt and put it in a string cout << line; } else{ cout<<“File does not exist!”<<endl; exit(1);} } openFile.close(); return 0; }
  • 17. Want to learn more about programming or Looking to become a good programmer? Are you wasting time on searching so many contents online? Do you want to learn things quickly? Tired of spending huge amount of money to become a Software professional? Do an online course @ baabtra.com We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.
  • 18. Follow us @ twitter.com/baabtra Like us @ facebook.com/baabtra Subscribe to us @ youtube.com/baabtra Become a follower @ slideshare.net/BaabtraMentoringPartner Connect to us @ in.linkedin.com/in/baabtra Give a feedback @ massbaab.com/baabtra Thanks in advance www.baabtra.com | www.massbaab.com |www.baabte.com
  • 19. Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com Contact Us