File

If we want to store our data or want to create a persistent of an object then it becomes necessary to store in a special data structure called file. Anything stored on permanent storage such as hard disk is called a file.

Record

A set of a related data item of a particular entity is called a record.

Field

An individual item of a record is called field.

Arrangement of  records in files:

  • Ascending and Descending order
  • Alphabetically order
  • Chronological order

Files and streams

A stream is a data flow from a source to a sink. The sources and sink of any input/output devices or files. For input and output, there are two different streams.
  • Standard input streams
  • Standard output streams
The standard source and sink are keyboard and monitor screen respectively. Whenever <iostream> included in the program these streams are initialized. we need to used the insertion and Extraction operator to implement these streams. The important stream classes are required for file.
  • f stream
  • of stream
  • if stream
IF stream:  It is an input stream class that is used to read files.If streams inherit the following functions.
  • get()
  • getline()
  • read()
  • seekg()
  • tellg()

Of streamIt is an output stream class that is used to Write files. Of streams inherit the following functions.
  • put()
  • writes()
  • seekp()
  • tellp()
F stream: It supports the input and output mode of the file. The function associated with this stream is:
  • Open()
  • Close()
  • closeall()
  • seekg()
  • seekp()
  • tellg()
  • tellp()


The header file "#include<iostream>" contains all the class declaration required for file I/O. 


Opening and Closing File(Text Files)

A file can be opened in c++ by two methods:
  • By using the constructor of the stream class 
  • By the open() function of the stream class

By using the constructor of the stream class:

Though opening a file through a constructor is an easy way but the following points should be kept in your mind.
Only one file can be connected to a stream
A file open in any  mode (input/output) must be close by the function close()

Write on the file:

 ofstream myfile("File name") 

//This statement open your file

myfile<<"Store in file";

myfile<<Variable name<<endl;

// These statements can store data in your file

Read  from the file:

 ifstream myfile("File name") 

//This statement open your file

Cout<<Variable name<<endl;

// This statement can read data from your file

Example Program:
Write a program that writes the following piece of text in a user-defined file ("thought.dat"). Verify the creation of the file by opening the same in output mode and printing its content on screen. 


#include<iostream>;
using namespace std;
int main()
{
char fname[20];
char text[] = "True education is that destroy narrow-mindedness";
char line[80];
int i;
cout<<"Enter the name of file to be opened"<<endl;
cin>>fname;
ofstream afile(fname);
for(int i = 0 ; i < 48 ; i ++ )
{
 afile.put(text[i]);
 afile.put( '  \n ' ); 
 afile.close()
 }
 ifstream xfile(fname);
 cout<<"The line read from the file"<<endl;
 xfile.getline(line,80);
 cout<<"\n"<<line;
 xfile.close();
}
}

By using the open() of the stream class:

The major advantage of this method is that more than one file can be opened at a time in the program.
These files can take less memory space from the storage of data.
These files are used to read or write structured data such as structures, class, and objects, etc.

Opening a file by using open function:

 ofstream myfile;

myfile.open("File name.dat");

Reading and writing blocks and objects(Binary files):

For reading a file:

<filename> . .read( (char *)this , sizeof(* this));

For writing a file:

<filename> . .write(char *)this , sizeof(* this));

Example Program:

Write a program that can take student Name, Roll no, Marks, and Grade store in a file and read from file By using the open function.

#include<iostream>;
#include<fstream>;
using namespace std;
class student
{
private:
char name[20];
int roll;
int marks;
char grade;
void compute_grade();
public:
void read_data();
void store_data();
void display_data();
};
void student :: read_data()
{
cout<<"Enter name"<<endl;
cin>>name;
cout<<"Enter roll no"<<endl;
cin>>roll;
cout<<"Enter marks"<<endl;
cin>>marks;
compute_grade();
}
void student :: store_data()
{
ofstream afile;
afile . open("filename",ios::app|ios::binary);
afile . write((char *)this , sizeof(* this));
}
void student :: display_data()
{
ifstream xfile;
xfile . open("filename",ios::app|ios::binary);
while(xfile)
{
cout<<name<<roll<<marks<<grade<<endl;
xfile . read((char*)this , sizeof(*this));
}
}
void student::compute_grade()
{
if(marks >= 80)
{
grade = ' A ';
else if(marks >= 70)
{
grade = ' B ';
else if(marks >= 60)
{
grade = ' C ';
else if(marks >= 50)
{
grade = ' D ';
else
{
grade = ' F ';
}
}
int main()
{
student s1;
s1.read_data();
s1.store_data();
s1.display_data();
}
Detecting End of File:
A situation can arise when we don't know the number of objects to be read from the file i.e we don't know where the file is going to end? A simple method of detecting the end of file is:

ifstream myfile;

myfile.open( filename )

while( < myfile> )

{
    ;
}
 The while loop in this program executes till eof()  of myfile is reached