File Handling
File Handling
Introduction
∙ Many real-life problems handle large volumes of
data.
∙ The data is stored in the devices using the concept of
files.
∙A file is a collection of related data
stored in a particular area on the disk.
∙ Programs are designed to perform read and
write operations on these files.
Console-Program-File interaction
Program File Communication
∙ In C++ file streams are used as an interface
between the program and the files.
∙ The stream that supplies data to the program is
known as input stream and the one that receives
data from the program is known as output stream.
ios
iostream
file
istream streambuf ostream
iostream
fstream base
Opening and Closing a File
∙ To open a file, a file stream is created and then it
is linked to the filename.
#include<iostream>
#include<ifstream>
using namespace std;
int main()
{
//Creating an input stream to read a file
ifstream ifstream_ob;
char ch;
//Reading the file using get() function and displaying its content
while(ifstream_ob)
{
ch = ifstream_ob.get();
cout<<ch;
}
//Closing
ifstream_ob.close();
return 0;
}
//C++ Writing data to a file using put() function and ios::out mode
#include<iostream>
#include<fstream>
#include<cstring>
char arr[100] = "Hello World. We wish you best in everything. Never give up!";
//Reading the char array i.e. a character at a time and writing it to the file
for(int i=0; i<length; i++)
{
ch = arr[i];
ofstream_ob.put(ch); //Writing a character to file, by using put() function
}
//Closing the output stream
ofstream_ob.close();
return 0;
}
TASK : Output ???
Opening Files Using open()
∙ The function open() can be used to open
multiple files that use the same stream object.
∙ Syntax:
File-stream-class stream-object;
stream-object.open(“filename”);
∙ A stream object can be connected to only one file
at a time.
TASK : FIND OUTPUT
#include<iostream.h> #include<fstream.h>
int main()
{
ofstream fout;
fout.open(“Country”);
fout.open(“Capital”);
fout<<“Washington”; fout<<“London”;
fout.close();
TASK : FIND OUTPUT
const int N=80;
char line[N];
ifstream fin;
fin.open(“Country”);
fin.open(“Capital”);
while(fin)
{
fin.getline(line, N);
cout<<line;
}
fin.close();
return 0;
}
NOTE:Opening two files simultaneously
using namespace std; // Declare the standard namespace for easier code readability.
int main() {
const int size = 80; // Define the maximum size for the character array.
if (!fin1.is_open() || !fin2.is_open()) {
cout << "Failed to open one or both of the files." << endl;
return 1;
}
int main() {
// Create an output file stream and open a file for writing
EXAMPLE
std::ofstream outputFile("example.txt");
if (!inputFile) {
std::cerr << "Failed to open the file for reading." << std::endl;
return 1;
}
return 0;
}
TASK
Where will the file pointer point in the following
statement?
infile.seekg(10);
int main()
{
char string[80];
cout << “Enter a string :\n ”;
cin >> string;
Put() & Get() functions
file.seekg(0);
Put() & Get() functions
char ch;
while(file)
{
file.get(ch);
cout << ch;
}
return 0;
}
Reading & Writing a class object
∙ Syntax:
▫infile.read ((char *) & V, sizeof (V));
▫outfile.write ((char *) & V, sizeof (V));
∙ The first argument is the address of the variable V.
∙ The second is the length of that variable in bytes.
∙ The address of the variable must type cast to char *
(ie: pointer to character type).
Example
#include<iostream.h>
#include<fstream.h> class
inventory
{
char name[10]; int code;
float cost; public:
void readdata(void);
void writedata(void);
};
Example
void inventory :: readdata(void)
{
cout<<“Enter name:”;
cin>> name;
cout<<“Enter code:”;
cin>>code;
cout<<“Enter cost:”; cin>> cost;
}
void inventory :: writedata(void)
{
cout<<name;
cout<<code;
cout<<cost;
}
int main()
{
inventory item[3];
fstream file;
file.open(“Stock.dat”, ios::in | ios::out);
cout<<“Enter the details for three items:”;
for(int i=0; i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i], sizeof(item[i]));
}
Example
file.seekg(0);
for(i=0;i<3;i++)
{
file.read((char *) & item[i], sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
Updating a File: Random Access
#include<iostream>
#include<fstream>
class inventory
{
char name[10];
int code;
float cost;
public:
void getdata(void)
{
cout<<“Enter name: ” ;cin>> name;
Updating a File: Random Access
void putdata(void)
{
cout<<name;
cout<<code;
cout<<cost;
}
};
int main()
{
inventory item;
fstream inoutfile;
inoutfile.open(“stock.dat”, ios::ate | ios::in | ios::out|
ios::binary);
Updating a File: Random Access
while(inoutfile.read((char * ) & item, sizeof item))
{
item.putdata();
}
inoutfile.clear(); //turn off EOF flag
cout<<“Add an item:”;
item.getdata();
inoutfile.write((char * ) & item, sizeof item);
inoutfile.seekg(0);
while(inoutfile.read((char * ) & item, sizeof item))
{
item.putdata();
}
Updating a File: Random Access
int last = inoutfile.tellg();
int n = last/sizeof(item);
cout<<“Number of objects:”<<n;
cout<<“Enter the object number to be updated:”;
int object;
cin>> object;
int location = (object-1)* sizeof(item);
inoutfile.seekp(location);
cout<<“Enter the new values of the object:”;
item.getdata();
inoutfile.write((char *) & item, sizeof item);
Updating a File: Random Access
inoutfile.seekg(0);
return 0;
}
Updating a File: Random Access
Output:
current contents of stock:
AA 11 100
BB 22 200
CC 33 300
Add an item:
Enter name: DD
Enter code: 44
Enter cost: 400
Updating a File: Random Access
Contents of Appended file:
AA 11 100
BB 22 200
CC 33 300
DD 44 400
Number of objects: 4
Enter the object to be updated: 4
Enter new values for object: Enter
name: EE
Enter code: 55 AA 11 100
Enter cost: 500 BB 22 200
Contents of updated file: CC 33 300
EE 55 500
Error Handling During File Operations
}
..........
..........