File Handling in C++
File: A file is a collection of related data stored in a
particular area on disk. For large amount of data, devices such as floppy disk,
hard disk etc are required, and data is stored in these devices using files.
C++ has standard library called
fstream, fstream defines three new classes or data type.
(1) ofstream: It is a output stream. This stream is used
for writing data on files.
(2) ifstream: It is a input stream. This stream is used for
reading data from files.
(3) fstream: This stream is used for both reading and writing
data from/to the files.
Usage of files
(1) Permanent storage of data
(2) Data transfer between two devices
Opening a file in C++
Before reading and writing any data
from/to the files, a file must be opened. For opening a file in c++, we have to
create an object of class ofstream, ifstream or fstream. If we trying to
opening a file, which is not existing in system, then c++ compiler
automatically create that file and open that file.
A file can be opened in two ways
(1) Using constructor
(2) Using open() function
Opening a file using constructor
Syntax:
file-stream-class
object-name("filename");
Example:
ofstream obj("testing.txt");
Opening a file using open function
Syntax:
file-stream-class object-name;
object-name.open("filename");
Example:
ifstream obj;
obj.open("testing.txt");
File opening modes
(1) ios::app - Append data at the end of file.
(2) ios::ate - When file is opened, takes the control at the end
of file
(3) ios::in - Open the file only for reading
(4) ios::out - Open the file only for writing data on file
(5) ios::trunc - Content of file is truncated, before file is
opened, if the file exists.
(6) ios::binary - Open the file in binary mode.
Text mode is default opening mode.
Example:
ofstream obj;
obj.open("testing.txt",
ios::app);
We can open a file in two or more
opening modes using | symbol. For example,
ofstream obj;
obj.open("testing.txt",
ios::app | ios::binary);
Closing a File in C++
When we finishes our work on file, then
we can close this file by the following syntax:
file-stream-class
object-name("filename");
object-name.close();
When c++ program terminates, it
automatically close our files, deallocate all memory reserved for our program.
But it is a good practice to close all files before program terminates.
Reading & Writing from/to File in
C++
Data can be read & written from/to
file in c++ using extraction operator(>>) and insertion
operator(<<).
Example:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream fout;
/*Open the file in
writing mode*/
fout.open("testing.txt", ios::out | ios::trunc);
char ch[100];
cout<<"Enter a
String: ";
cin.getline(ch, 100);
fout<<ch; //String
is written on file
fout.close(); //File is
closed
/*Open the file in
reading mode*/
ifstream
fin("testing.txt", ios::in);
char r;
cout<<"\nYou
have entered following the string\n\n";
r=fin.get();
while(!fin.eof())
{
cout<<r;
r=fin.get();
}
fin.close();
return 0;
}
OUTPUT:
Enter a String: Pc Technical Pro
You have entered following the string
Pc Technical Pro
0 comments:
Post a Comment