SlideShare a Scribd company logo
4
Most read
6
Most read
7
Most read
File handling in c++
FILE HANDLING IN C++
NikhilDevS.B
nikhildevsb@gmail.com
www.facebook.com/nikhildevsb
TwitterProfile
www.linkedin.com/nikhildevsb
Typing speed: 22 wpm.
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 of information, usually stored on a
computerโ€™s disk. Information can be saved to files and then
later reused.
โ€ข Computers store files to secondary storage so that the
contents of files remain intact when a computer shuts down.
โ€ขWhen a computer reads a file, it copies the file from the
storage device to memory; when it writes to a file, it transfers
data from memory to the storage device.
โ€ขAll files are assigned a name that is used for identification
purposes by the operating system and the user.
Files
Diskette
Memory
Input file
Output file
Used to transfer data to and from disk
Steps in Processing 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.
File streams in c++
The following classes in c++ have access to file input and output
functions:
ifstream - This data type represents the input file stream and is used to read
information from files
ofstream - This data type represents the output file stream and is used to
create files and to write information to files.
fstream - This data type represents the file stream generally, and has the
capabilities of both ofstream and ifstream which means it can
create files, write information to files, and read information
from files.
Opening Files Using Constructor
ofstream - output file stream
โ€ขFile access requires the inclusion of header file fstream.h
โ€ข Before data can be written to or read from a file, the file must be opened.
โ€ข Use ofstream for output stream.
ofstream objectname( โ€œfilenameโ€);
E.g. // ofstream constructor opens file named โ€œcustomerโ€ for output(write).
fout as ofstream object.
void main()
{
ofstream fout( "customerโ€); //open file to write
string str_name=โ€œJohnโ€;
fout<<string_name; //write to file
โ€ฆโ€ฆ.
}
ifstream โ€“ input file stream
ifstream for input stream.
E.g. Void main()
{
โ€ฆ..
โ€ฆ..
ifstream fin( โ€œcustomerโ€);
fin>>string_name; //read from file
โ€ฆโ€ฆ.
โ€ฆโ€ฆ.
}
Opening Files using open()
โ€ข Open multiple files using same stream object.
โ€ข syntax:
file-stream-class stream-object;
stream-object.open(โ€œfilenameโ€,filemode);
Example:
ofstream fout;
fout.open(โ€œcustomer1โ€);
...........
...........
fout.close();
fout.open(โ€œcustomer2โ€);
.........
.........
fout.close();
optional
File Open Modes
ios:: in - (input) open a file to read
ios::out - (output) open a file to write
ios:: app - append to end-of-file.
ios: trunc -(truncate) delete the file contents if it exists.
ios:nocreate โ€“ open fails if the file doesnโ€™t exist.
ios:noreplace โ€“ open fails if file already exists.
Example for ios::out mode
#include <fstream.h>
int main(void)
{
//opens a file named file1.txt in write mode
ofstream outFile("file1.txt", ios::out); //constructor type
outFile << โ€œitโ€™s a file"; //write to file
outFile.close(); //file is closed
return 0;
}
fstream โ€“ input and ouput file stream
โ€ข fstream object can handle both the input and output(read
and write) simultaneously.
void main()
{
string name=โ€œJohnโ€;
fstream f; //created object f for fstream;
f.open(โ€œTEXTโ€, ios :: in |ios :: out); //opens file TEXT in read & write mode
f<<name; // write name to file
f>>name; // read name from file
cout<<name;
}
Closing a File
โ€ข When we finish with a mode, we need to close the file
before ending the program or beginning another mode
with that same file.
โ€ขTo close a file, we use close using the object:
f.close();
Function for Manipulation of File Pointer
If we want to move desired position on the file we use certain
built in functions:
โ€ข seekg() : seekget , moves getpointer(read) to specified
location.
syntax: seekg(offset,refposition);
E.g.
f1.seekg(0, ios::beg); - go to start.
f1.seekg(0,ios::cur); - stay at the current position
f1.seekg(0,ios::end); - go to end of file.
f1.seekg(m,ios::beg); - move to (m+1)th byte in the file.
f1.seekg(m,ios::cur) ; - go forward by m byte from current
position
โ€ข seekp() : seekput, move putpointer(write) to
desired position.
syntax: seekp(offset,refposition);
โ€ข tellg() : gives the current position of getpointer.
โ€ข tellp() : gives the current position of putpointer.
โ€ขEg.
ofstream fout; //outstream object fout
fout.open(โ€œhellowโ€,ios::app); //open append mode and fout
points last postion
int p=fout.tellp(); //return number of bytes in file
Thank you...
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

PPT
Chapter Introduction to Modular Programming.ppt
PPTX
Strings in c++
PPTX
STACKS IN DATASTRUCTURE
PPTX
Inheritance in c++
PDF
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
PPT
Class and object in C++
PPT
Strings in c
PPT
Active and passive voice
Chapter Introduction to Modular Programming.ppt
Strings in c++
STACKS IN DATASTRUCTURE
Inheritance in c++
Chap 8 The stack and introduction to procedures & Chapter 9 multiplication an...
Class and object in C++
Strings in c
Active and passive voice

What's hot (20)

PPTX
File Handling Python
PPTX
PPT
Data structures using c
PPTX
Constructor and Types of Constructors
PPTX
Inheritance in c++
PPTX
File in C language
PDF
C++ OOPS Concept
PPSX
Javascript variables and datatypes
PPTX
Pointers in c++
PPTX
Method overloading
PPT
File handling in c
PPT
Input and output in C++
PPTX
Polymorphism In c++
PPTX
classes and objects in C++
PDF
Managing I/O in c++
PPT
Basic concept of OOP's
PPTX
File handling in Python
ย 
PPTX
Stream classes in C++
PPTX
Data types in java
PDF
Arrays in Java
File Handling Python
Data structures using c
Constructor and Types of Constructors
Inheritance in c++
File in C language
C++ OOPS Concept
Javascript variables and datatypes
Pointers in c++
Method overloading
File handling in c
Input and output in C++
Polymorphism In c++
classes and objects in C++
Managing I/O in c++
Basic concept of OOP's
File handling in Python
ย 
Stream classes in C++
Data types in java
Arrays in Java
Ad

Similar to File handling in c++ (20)

PDF
Files and streams
PPT
Data file handling
PDF
637225560972186380.pdf
PPTX
Pf cs102 programming-8 [file handling] (1)
PDF
Filesinc 130512002619-phpapp01
ย 
PPSX
Files in c++
PPTX
File Handling
PPTX
File Handling
PPTX
Files in c++
PPTX
Reading and Writing Files
PDF
Unit 5 File handling in C programming.pdf
PPT
How to do file-handling - in C language
PPTX
Basics of file handling
PPTX
basics of file handling
PPTX
File handling
PPTX
Chapetr 4 C++ file object oriented programming
PPT
PDF
File operations
PPT
File Handling Btech computer science and engineering ppt
Files and streams
Data file handling
637225560972186380.pdf
Pf cs102 programming-8 [file handling] (1)
Filesinc 130512002619-phpapp01
ย 
Files in c++
File Handling
File Handling
Files in c++
Reading and Writing Files
Unit 5 File handling in C programming.pdf
How to do file-handling - in C language
Basics of file handling
basics of file handling
File handling
Chapetr 4 C++ file object oriented programming
File operations
File Handling Btech computer science and engineering ppt
Ad

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

PPTX
Agile methodology and scrum development
PDF
Acquiring new skills what you should know
PDF
Baabtra.com programming at school
PDF
99LMS for Enterprises - LMS that you will love
PPTX
PPTX
Chapter 6 database normalisation
PPTX
Chapter 5 transactions and dcl statements
PPTX
Chapter 4 functions, views, indexing
PPTX
Chapter 3 stored procedures
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PPTX
Chapter 1 introduction to sql server
PPTX
Chapter 1 introduction to sql server
PPTX
Agile methodology and scrum development
Acquiring new skills what you should know
Baabtra.com programming at school
99LMS for Enterprises - LMS that you will love
Chapter 6 database normalisation
Chapter 5 transactions and dcl statements
Chapter 4 functions, views, indexing
Chapter 3 stored procedures
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server

File handling in c++

  • 2. FILE HANDLING IN C++ NikhilDevS.B nikhildevsb@gmail.com www.facebook.com/nikhildevsb TwitterProfile www.linkedin.com/nikhildevsb Typing speed: 22 wpm.
  • 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 of information, usually stored on a computerโ€™s disk. Information can be saved to files and then later reused. โ€ข Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down. โ€ขWhen a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device. โ€ขAll files are assigned a name that is used for identification purposes by the operating system and the user.
  • 5. Files Diskette Memory Input file Output file Used to transfer data to and from disk
  • 6. Steps in Processing 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. File streams in c++ The following classes in c++ have access to file input and output functions: ifstream - This data type represents the input file stream and is used to read information from files ofstream - This data type represents the output file stream and is used to create files and to write information to files. fstream - This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
  • 8. Opening Files Using Constructor ofstream - output file stream โ€ขFile access requires the inclusion of header file fstream.h โ€ข Before data can be written to or read from a file, the file must be opened. โ€ข Use ofstream for output stream. ofstream objectname( โ€œfilenameโ€); E.g. // ofstream constructor opens file named โ€œcustomerโ€ for output(write). fout as ofstream object. void main() { ofstream fout( "customerโ€); //open file to write string str_name=โ€œJohnโ€; fout<<string_name; //write to file โ€ฆโ€ฆ. }
  • 9. ifstream โ€“ input file stream ifstream for input stream. E.g. Void main() { โ€ฆ.. โ€ฆ.. ifstream fin( โ€œcustomerโ€); fin>>string_name; //read from file โ€ฆโ€ฆ. โ€ฆโ€ฆ. }
  • 10. Opening Files using open() โ€ข Open multiple files using same stream object. โ€ข syntax: file-stream-class stream-object; stream-object.open(โ€œfilenameโ€,filemode); Example: ofstream fout; fout.open(โ€œcustomer1โ€); ........... ........... fout.close(); fout.open(โ€œcustomer2โ€); ......... ......... fout.close(); optional
  • 11. File Open Modes ios:: in - (input) open a file to read ios::out - (output) open a file to write ios:: app - append to end-of-file. ios: trunc -(truncate) delete the file contents if it exists. ios:nocreate โ€“ open fails if the file doesnโ€™t exist. ios:noreplace โ€“ open fails if file already exists.
  • 12. Example for ios::out mode #include <fstream.h> int main(void) { //opens a file named file1.txt in write mode ofstream outFile("file1.txt", ios::out); //constructor type outFile << โ€œitโ€™s a file"; //write to file outFile.close(); //file is closed return 0; }
  • 13. fstream โ€“ input and ouput file stream โ€ข fstream object can handle both the input and output(read and write) simultaneously. void main() { string name=โ€œJohnโ€; fstream f; //created object f for fstream; f.open(โ€œTEXTโ€, ios :: in |ios :: out); //opens file TEXT in read & write mode f<<name; // write name to file f>>name; // read name from file cout<<name; }
  • 14. Closing a File โ€ข When we finish with a mode, we need to close the file before ending the program or beginning another mode with that same file. โ€ขTo close a file, we use close using the object: f.close();
  • 15. Function for Manipulation of File Pointer If we want to move desired position on the file we use certain built in functions: โ€ข seekg() : seekget , moves getpointer(read) to specified location. syntax: seekg(offset,refposition); E.g. f1.seekg(0, ios::beg); - go to start. f1.seekg(0,ios::cur); - stay at the current position f1.seekg(0,ios::end); - go to end of file. f1.seekg(m,ios::beg); - move to (m+1)th byte in the file. f1.seekg(m,ios::cur) ; - go forward by m byte from current position
  • 16. โ€ข seekp() : seekput, move putpointer(write) to desired position. syntax: seekp(offset,refposition); โ€ข tellg() : gives the current position of getpointer. โ€ข tellp() : gives the current position of putpointer. โ€ขEg. ofstream fout; //outstream object fout fout.open(โ€œhellowโ€,ios::app); //open append mode and fout points last postion int p=fout.tellp(); //return number of bytes in file
  • 18. 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.
  • 19. 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
  • 20. 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