Data File Handling in C++


Data File Handling in C++

Data File Handling in C++


What is File?

A file is a stream of bytes stored on some secondary storage devices.

There are two type of data file-

1.    Text file
2.   Binary file



1.  Text file

 A text file stores information in the readable and printable form. Each line of text is terminated with an EOL(End of line character).


2. Binary File

A binary file contains information in the non-readable i.e. in some format in which it is held in memory.


Stream

A stream is used to name flow of data. There are three file I/O classes used for file read/write.

1. ifstream   -                                    used for read operation
2. ofstream  -                                   used for write operation
3. fstream     -                                   used for both read/write


fstream.h

This header file includes the definition of stream classes ifstream, ofstream, fstream. It contains predefined set of operation for handling file.



A file can be opened


1.  By the constructor method

This method is preferred when file is opened in input or output mode.
Ex. ofstream file(“student.dat”);


2.  By the member function

It will prefer when file is opened in various mode i.e. ios::in, ios::out, ios::app, ios::ate etc.

Ex. fstream file;
file.open(“book.txt”,ios::in|ios::out|ios::binary);



File modes

ios::out

It opens the file in output mode and place the file pointer in beginning.


ios::in
It opens the file in read mode.


ios::app
It opens the file in write mode and place the file pointer at the end.


ios::ate
It opens the file in read/write mode and place the file pointer at the end of file i.e. input/output operation perform anywhere in the file.


ios::trunc
It empties the existing file.


ios::noreplace
If file doesn’t exist, a new file gets created but if exist, the open fails.


 ios::nocreat
If file doesn’t exist, this shows no file is created and open fails.


ios::binary
Open a file in binary mode.


eof()
This function determines end of file by returning true.


close ()
This function terminates connection between file and stream and release memory.

Syntax
stream_object.close( );

Ex. file.close( );


 General program structure for creating text file.

#include<fstream.h>
#includes<iostreamh.>
#include<stdio.h>
void main()
{
 char s [80],ch=’y’;
ofstream file (“myfile.txt”)                 //open my file in default output mode
while (ch==’y’||ch==’Y’)
{
   cout<<”\n Enter line of text”;
   gets(s);
   file<<s;
   cout<<”\n More input y/n”;
   cin>>ch;
  }
file.close();
}


Text file functions

Char I/O-

get ()
Read a single character from text file and store in a buffer.
Ex. file.get(ch);


Read the content of ‘myfile.txt’ and display it on monitor.

#include<fstream.h>
#include<iostream.h>
#include<stdio.h>
void main()
{
  char ch;
ifstream file(“myfile.txt”);               //open “myfile.txt” in default input mode
while (file)
 {
  file.get(ch);                                    //read the character from file
 cout<<ch;
}
file.close();
}                                                     //end of main      
put()
Writing a single character in textfile.
Ex. file.put(ch);
getline ()
Read line of text from text file.
Ex. file.getline(s,80);
Note-We can also use file>>ch for reading and file>>ch writing in text file.


Binary File Function

read( )-read a block of binary data.

Syntax
stream_object.read((char*)&object,size_of_object));
Ex.file.read((char*)&s,sizeof(s));



write( )

Write a block of binary data on fixed number or fixed number of bytes.
Syntax
stream_object.write(char*)&object ,sizeof(object));
Ex. file.write(char*)&, sizeof(s));



File pointer

The file pointer indicates the position in the file at which the next input/output is occur.

Moving the file pointer for various operations, following functions are used.



1. seekg ()

It places the file pointer to specified position in input mode of file.

Ex. file.seekg(p,ios::beg)or file.seekg(-p,ios::end),

Or

 file.seekg(p,ios::cur)

i.e. to move p byte position from beginning, end and current position.



2. seekp()

It places the file pointer to specified position in output mode.

Ex. file.seekp(p,ios::beg) or file.seekp(-p,ios::end)

Or file.seekp(p,ios::cur)



3. tellg()

This function returns the current working position of file pointer in output mode.

Ex. int p=file.tellp();



Binary file operation- create, read, write, search, delete and modify.

#include<iostream.h>
#include<fstream.h>
#include<cstdio.h>
class student
{
 int admno;
char name[50];
public:
void setData()
{
 cout<<”\n Enter admission no.”;
cin>>admno;
cout<<”Enter name of student”;
cin.getline(name,50);
}
void show()
{
 cout<<”\n Admission no:”<<admno;
cout<<”\n Student Name:”<<name;
}
int retAdmno()
{
 Return admno;
}
};
/*function to write in binary file*/
void write_record()
{
 ofstream outfile;
ostream.open(“student.dat”,ios::binary|ios::app);
student obj;
obj.setData();
outfile.write((char*)&obj,sizeof(obj));
outfile.close();
}
/*function to display records of file*/
void display()
{
 ifstream infile;
infile.open(“student.dat”,ios::binary);
student obj;
while (infile.read((char*)&obj,sizeof(obj)))
{
 obj.showData();
}
infile.close();
}
/*A function to search and display from binary file*/
void search(int n)
{
 ifstream infile;
infile.open(“student.dat”,ios::binary);
student obj;
while infile.read((char*)&obj,sizeof(obj)))
 {
   if(obj.retAdmno()==n)
 {
   obj.showData();
}
}
infile.close();
}
/*function to delete a record*/
void delete_record(int n)
{
 Student obj;
 ifstream infile;
infile.open(“student.dat ”,ios::binary)
ofstream outfile;
outfile.open(“temp.dat”, ios::out|ios::binary);
while(infile.read((char*)&obj,sizeof(obj)))
{
 if(obj.retAdmno()==n)
{
 cout<<”\n Enter the new detail of student”;
obj.setData();
int pos=-1*sizeof(obj);
file.seekp(pos,ios::cur);
file.write((char*)&obj,sizeof(obj));
}
}
file.close();
}




This post is about Data File Handling in C++. In the next post we will discuss the topic Pointer . 





 








      




Post a Comment

0 Comments