file handling in cpp

19

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 12-Jul-2015

134 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: File handling in cpp
Page 2: File handling in cpp

FILE HANDLING IN C++

SOBIN [email protected]

Sobin jose

Sobin jose

Typing speed:21wpm

Page 3: File handling in cpp

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 cpp

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.

Page 5: File handling in cpp

Classes for file stream operation ofstream: Stream class to write on filesifstream: Stream class to read from filesfstream: Stream class to both read and write from/to files.

Page 6: File handling in cpp

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.

Page 7: File handling in cpp

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”);

Page 8: File handling in cpp

// 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 open\n";

cout << "or create: ";

cin.getline(fileName, 81);

dataFile.open(fileName, ios::out);

cout << "The file " << fileName << " was opened.\n";

}

Page 9: File handling in cpp

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.

Page 10: File handling in cpp

#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;

}

Page 11: File handling in cpp

INPUT AND OUTPUT OPERATIONput() and get() functionthe 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() functionwrite() and read() functions write and read blocks of binary data.example:file.read((char *)&obj, sizeof(obj));file.write((char *)&obj, sizeof(obj));

Page 12: File handling in cpp

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 istreamand ostream).

Page 13: File handling in cpp

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 );

Page 14: File handling in cpp

•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 fileios::cur current position of the pointerios::end end of the file•example:file.seekg(-10, ios::cur);

Page 15: File handling in cpp

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 fileFile.seekg(ios::beg); //go to the beginning of the fileFile.get(ch); //read one charactercout << ch << endl; //display itFile.close();

}

Page 16: File handling in cpp

File I/O Example: Reading#include <iostream>#include <fstream>#include <string>

int main(void){

ifstream openFile(“data.txt"); //open a text file data.txtstring 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;}

Page 17: File handling in cpp

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 18: File handling in cpp

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 19: File handling in cpp

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