file handling in c++
TRANSCRIPT
![Page 1: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/1.jpg)
![Page 2: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/2.jpg)
FILE HANDLING IN C++
Nikhil Dev S.B
www.facebook.com/nikhildevsb
Twitter Profile
www.linkedin.com/nikhildevsb
Typing speed : 22 wpm.
![Page 3: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/3.jpg)
Disclaimer: This presentation is prepared by trainees ofbaabtra.com as a part of mentoring program. This is notofficial document of baabtra.com – Mentoring Partner
![Page 4: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/4.jpg)
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.
![Page 5: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/5.jpg)
Files
DisketteMemory
Input file
Output file
Used to transfer data to and from disk
![Page 6: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/6.jpg)
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.
![Page 7: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/7.jpg)
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.
![Page 8: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/8.jpg)
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 writestring str_name=“John”;fout<<string_name; //write to file
…….
}
![Page 9: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/9.jpg)
ifstream – input file stream
ifstream for input stream.E.g. Void main()
{…..…..ifstream fin( “customer”); fin>>string_name; //read from file…….…….}
![Page 10: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/10.jpg)
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
![Page 11: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/11.jpg)
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.
![Page 12: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/12.jpg)
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;}
![Page 13: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/13.jpg)
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 filef>>name; // read name from filecout<<name;}
![Page 14: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/14.jpg)
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();
![Page 15: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/15.jpg)
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 positionf1.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
![Page 16: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/16.jpg)
• 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
![Page 17: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/17.jpg)
Thank you...
![Page 18: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/18.jpg)
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 designedto make a quick, good software professional out of anybody who holds a passion for coding.
![Page 19: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/19.jpg)
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
![Page 20: File handling in c++](https://reader031.vdocument.in/reader031/viewer/2022013111/55a2430f1a28abf1448b46b1/html5/thumbnails/20.jpg)
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode
Start up VillageEranakulam,Kerala, India.
Email: [email protected]
Contact Us