Data File Handling in C++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Data File Handling

File handling in c++


File Handling In C++
Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output
of a program in a file and to perform various operations on it.

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.
So we should include <fstream.h> header file .

C++ provides us with the following operations in File Handling:


•Creating/Open a file: open()
•Reading data: read()
•Writing new data: write()
•Closing a file: close()
File handling in c++
File Handling Function:
• open()
• close()
• getline()
• eof()
1. open(): used to create open the file. Its check the existing of file if file not exist it creates, otherwise open the
file.
Syntax: object.open(“path+filename”,[mode]);
Ex: ofstream out;
out.open(“d:/a1.txt”);
2. close(): used to close the opend file. It doesn’t take any argument.
Syntax: object.close( );
Ex: fstream f.open();
f.close();
File handling in c++
3. getline(): it’s a member of ifstream class. It is used to read data from file line by line. It take two arguments. One
is array variable,and second is size.
Syntax: object.getline( );
Ex: ifstream read;
read.getline(data,100);

4.eof(): its stands for end of file. It checks for the end-of-file condition before processing data read from an input
file stream.its a member of ifstream class.
Syntax: obj.eof();
Ex: while (!obj.eof( )) //if not at end of file, continue reading numbers
{
cout<<data<<endl; //print numbers to screen
obj>> data; //get next number from file
}
File handling in c++
We can open a file using any one of the following methods:
1. Using constructor (bypassing the file name in constructor at the time of object creation.)
2. Using the open() function.
1. Using constructor :
We know that a constructor of class initializes an object of its class when it (the object) is being created. Same way, the
constructors of stream classes (ifstream, ofstream, or fstream) are used to initialize file stream objects with the filenamespassed
to them. This is carried out as explained here:
To open a file named myfile as an input file (i.e., data will be need from it and no other operation like writing or modifying
would take place on the file).
we shall create a file stream object of input(read) type like:
ifstream fin(“myfile",[ ios::in]) ;
or
ifstream in(“myfile”);
we shall create a file stream object of output(write) type like:
ofstream fout ("myfile",[ ios::out]) ;
or
ofstream out (“myfile”);
File handling in c++
Ex:
#include<fstream.h>
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");

// Write to the file


MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();
}
File handling in c++
2. Using open function:
Another method for open or create the file is open function.A file must be opened before you can read from it or write to it.
Either ofstream or fstream object may be used to open a file for writing. Andeithers ifstream or fstream object is used to open
a file for reading purpose only.
Syntax: obj.open (filename, [mode]);

There are some mode flags used for file opening. These are:
•ios::app: append mode
•ios::ate: open a file in this mode for output and read/write controlling to the end of the file
•ios::in: open file in this mode for reading
•ios::out: open file in this mode for writing
•ios::trunk: when any file already exists, its contents will be truncate.

Reading from and Writing to file:


While doing C++ program, programmers write information to a file from the program using the stream insertion operator (<<)
and reads information using the stream extraction operator (>>).
The only difference is that for files programmers need to use an ofstream or fstream object instead of the cout object and ifstream
or fstream object instead of the cin object.
Example:
File handling in c++
void main()
{
clrscr();
ifstream in;
ofstream out;
out.open("d:/abc1.txt",ios::out);
out<<"world \n";
out<<"java \n";
out.close();
in.open("d:/abc1.txt",ios::in);
char data[100];
while(!in.eof())
{
in.getline(data,100);
cout<<data;
}
in.close();
getch();
}
File handling in c++
File Pointers:
In C++ we have a get pointer and a put pointer for getting (i.e. reading) data from a file and putting(i.e. writing)
data on the file respectively.
seekg()
seekp()
tellg()
tellp()

1. seekg(): seekg() is used to move the get pointer to a desired location with respect to a reference point.
Syntax: file_pointer.seekg (number of bytes ,Reference point);
Example: fin.seekg(10,ios::beg);
2. seekp(): seekp() is used to move the put pointer to a desired location with respect to a reference point.
Syntax: file_pointer.seekp(number of bytes ,Reference point);
Example: fout.seekp(10,ios::beg);
File handling in c++
3. tellg(): tellg() is used to know where the get pointer is in a file.
Syntax: file_pointer.tellg();
Example: int posn = fin.tellg();

4. tellp(): tellp() is used to know where the put pointer is in a file.


Syntax: file_pointer.tellp();
Example: int posn=fout.tellp();

You might also like