File Handling in C++

Views:
 
     
 

Presentation Description

No description available.

Comments

By: pallavip06 (23 month(s) ago)

I would like a copy of this presentation. Please send a copy to me at pallavi200745@yahoo.com if possible. Thanks

By: tindo (26 month(s) ago)

give me your email @tendaishava@gmail.com en will attach it to you......failed to change it to downloadable

By: tush1232000 (26 month(s) ago)

please allow me to download it .......please!!!!

By: rahul.king1990 (26 month(s) ago)

i need this PPT

By: imrankhan00001 (27 month(s) ago)

i need thus ppt

Presentation Transcript

File Handling in C++ : 

File Handling in C++

File Handling in C++ : 

File Handling in C++ Introduction to File Handling Data entered once and required later again. Same data to be used by others. Data required again by the same program.

Files and Streams : 

Files and Streams Program          Input Stream Output Stream           Files

I/O Streams : 

I/O Streams Stream Description cin Standard input stream cout Standard output stream cerr Standard error stream

File I/O Streams : 

File I/O Streams Stream classes required for File I/O : ifstream ofstream fstream

ifstream : 

ifstream Input file stream class Open() is a member function of the class ifstream Inherited functions of ifstream class from the class istream are: get() getline() read() seekg() tellg()

Ofstream : 

Ofstream Output file stream class. Open() is a member function of the class ofstream. Inherited functions of ofstream class from the class ostream are put() write() seekp() tellp()

fstream : 

fstream Supports files for simultaneous input and output. fstream is derived from ifstream ofstream iostream They are all parent class and fstream is the child class

Fstream : 

Fstream Member functions for fstream are: open close closeall seekg seekp tellg tellp

Opening a File : 

Opening a File A file can be opened by using any of the following two ways: Using the constructor function of the class Using the member function open() of the class

OPENING A FILE USING CONSTRUCTOR: : 

OPENING A FILE USING CONSTRUCTOR: As we know a constructor is used to initialize an object while it is being created. Same as we can open a file by supplying the name of the file to the constructor of the file stream as follows: Example: ofstream obj(“abc.jpg”); ifstream file(“LETTER.TXT”); fstream fileio(“answers.txt”);

OPENING A FILE USING OPEN() : 

OPENING A FILE USING OPEN() As we know the function open() is the member function in all the file streams ( ifstream,ofstream & fstream). We can open a file of a particular type of file stream with its default mode or by providing the other modes explicitly, as follows: Example: ifstream obj; obj.open(“STORY.TXT”); ofstream file; file.open(“product.dat”); fstream abc; abc.open(“xyz.dat”,ios::binary|ios::io|ios::out);

CLOSING A FILE: : 

CLOSING A FILE: A file is closed using the member function close(), as follows: Example: ifstream obj; ----------- ----------- obj.close();

FILE MODES: : 

FILE MODES: The file mode parameter in open() and constructor function specifies the purpose for which the file is opened. If we don’t supply the mode parameter when we open a file it uses the default values in the absence of the actual value. The default values are as follows: ios::in for ifstream functions meaning open for reading only. ios::out for ofstream functions meaning open for writing only.

OTHER FILE MODES & THEIR MEANING: : 

OTHER FILE MODES & THEIR MEANING: 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

SEQUENTIAL INPUT AND OUTPUT OPERATIONS: : 

SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 1.The file stream classes support a number of member functions for performing the input and output operations on files. There are two pair of functions for this purpose-put() & get() functions : these functions are designed for handling a single character at a time. syntax: obj.put(ch); obj.get(ch);

SEQUENTIAL INPUT AND OUTPUT OPERATIONS: : 

SEQUENTIAL INPUT AND OUTPUT OPERATIONS: 2. write() & read() functions: these functions are designed to write and read blocks of binary data. syntax: obj.write((char*)&var, sizeof(var));obj.read((char*)&var, sizeof(var)); Here var is an variable of either class or structure.