CBSE Class 12th Computer Science: Data File Handling in C++
CBSE Class 12th Computer Science: Data File Handling in C++
CBSE Class 12th Computer Science: Data File Handling in C++
File
File Stream
Stream: A stream is a general term used to name flow of data. Different streams are
used to represent different kinds of data flow.
There are three file I/O classes used for file read / write operations.
ifstream - can be used for read operations.
ofstream - can be used for write operations.
fstream - can be used for both read & write operations.
fstream.h:
This header file includes the definitions for the stream classes ifstream, ofstream and
fstream. In C++ file input output facilities implemented through fstream.h header file.
It contain predefines set of operation for handling file related input and output,
fstream class ties a file to the program for input and output operation.
A file can be opened using:
By the constructor method. This will use default streams for file input or
output. This method is preferred when file is opened in input or output mode
only. Example : ofstream file(“student.dat”); or ifstream file(“student.dat”);
By the open() member function of the stream. It will preferred when file is
opened in various modes i.e ios::in, ios::out, ios::app, ios::ate etc. e.g fstream
file;
file.open(“book.dat”, ios::in | ios::out | ios::binary);
File modes:
eof(): This function determines the end-of-file by returning true(non-zero) for end of file
otherwise returning false(zero).
close(): This function terminates the connection between the file and stream associated with
it.
Char I/O :
get() – read a single character from text file and store in a buffer.
e.g file.get(ch);
put() - writing a single character in textfile e.g. file.put(ch);
getline() - read a line of text from text file store in a buffer.
e.g file.getline(s,80);
We can also use file>>ch for reading and file<<ch writing in text file. But >> operator
does not accept white spaces.
Note:
The first is the address of variable, and the second is the length of that variable in
bytes. The address of variable must be type cast to type char*(pointer to character
type)
The data written to a file using write( ) can only be read accurately using read( ).
File Pointer: The file pointer indicates the position in the file at which the next input/output
is to occur.
Moving the file pointer in a file for various operations viz modification, deletion ,
searching etc. Following functions are used:
seekg(): It places the file pointer to the specified position in input mode of file. e.g
file.seekg(p,ios::beg); or file.seekg(-p,ios::end), or file.seekg(p,ios::cur)
i.e to move to p byte position from beginning, end or current position.
seekp(): It places the file pointer to the specified position in output mode of file.
e.g file.seekp(p,ios::beg); or file.seekp(-p,ios::end), or file.seekp(p,ios::cur)
i.e to move to p byte position from beginning, end or current position.
tellg(): This function returns the current working position of the file pointer in the input
mode.
e.g int p=file.tellg();
tellp(): This function returns the current working position of the file pointer in the output
#include<fstream.h>
struct student
{
char name[15];
float percent;
#include<fstream.h>
struct student
{
char name[15];
float percent;
};
Consider the following class declaration then write c++ function for following file
operations viz create_file, read_file, add new records, modify record, delete a record,
search for a record.
#include<iostream.h>
class student
{
int rno;
char name[30];
int age;
public:
void input()
{
cout<<”\n enter roll no”;
cin>>rno;
cout<<”\n enter name “;
gets(name);
cout<<”\n enter age”;