18 Files
18 Files
M. Anand M.Tech.
Assistant Professor (Sr. G)
Dept. of Information Technology
SRM University
Introduction
• A file is a collection of related data
stored in a particular area on the disk
• There are two kinds of data
communication
1. data transfer between the console unit
and the program .
2. data transfer between the program and
a disk file
Introduction
• Types of streams
1. Standard input/output
2. File input/output
3. String input/output
File Streams
• File stream classes:
– ifstream
• Used for input operations
• Derived from istream class
• Contains open() function with default input
mode
– ofstream
• Used for output operations
• Derived from ostream class
• Contains open() function with default output
mode
File Streams
– fstream
• Used for both input and output operations
• Derived from iostream class
• Inherites properties of both the istream and
the ostream classes
• Contains open() function with default input
mode
Classes for file stream operations
30-Sep-14 6 M. Anand
Classes for file stream operations
30-Sep-14 7 M. Anand
Opening and closing a file
A file can be opened in two ways :
1. using the constructor function of the
class.
2. using the member function open() of
the class.
30-Sep-14 8 M. Anand
Opening files using constructor
ofstream outfile(“result.txt”);
outpfile.close();
30-Sep-14 9 M. Anand
Opening files using constructor
#include<iostream.h>
void main()
{
ofstream outfile(“test.txt”);
outfile<<”This is a test\n”;
outfile<<”program to store\n”;
outfile<<”a set of lines on to a file\n”;
outfile.close();
}
30-Sep-14 10 M. Anand
Opening files using open()
file_stream_class stream_object;
stream_object.open(“filename”);
#include<iostream.h>
void main()
{
ofstream outfile;
outfile.open(“test.txt”);
outfile<<”This is a test\n”;
outpfile<<”program to store\n”;
outpfile<<’ a set of lines on a file\n”;
}
30-Sep-14 11 M. Anand
Detecting end-of-file
1. while(infile.eof()!=0)
{
…..
…..
…..
}
30-Sep-14 12 M. Anand
Detecting end-of-file
2. while (!infile.eof())
{
…..
…..
…..
}
30-Sep-14 13 M. Anand
Detecting end-of-file
3. while (infile)
{
…..
…..
…..
}
30-Sep-14 14 M. Anand
Detecting end-of-file
if (infile.eof()!=0)
{
exit(0);
}
30-Sep-14 15 M. Anand
More about open(): File modes
Stream_object.open(“filename”,mode);
30-Sep-14 16 M. Anand
More about open(): File modes
Eg:
fout.open(“data.txt”,ios::app||ios::nocreate);
30-Sep-14 17 M. Anand
File pointers and their manipulations
• Types of pointers
– Get pointer
– Put pointer
Default actions
30-Sep-14 18 M. Anand
Functions for manipulation of
file pointers
30-Sep-14 19 M. Anand
Functions for manipulation of file
pointers
seekg(offset,refposition);
seekp(offset,refposition);
Refposition
• ios::beg- start of the file
• ios::cur- current position of the pointer
• ios::end- end of the file
30-Sep-14 20 M. Anand
Functions for manipulation of file
pointers
30-Sep-14 21 M. Anand
Sequential input and output
operations
• put()
• get()
• read()
• write()
30-Sep-14 22 M. Anand
get()
char_variable=object_name.get();
#include<fstream.h>
void main()
{
char ch;
ifstream infile(“test.txt”);
while(infile)
{
infile.get(ch);
cout<<ch;
}
}
30-Sep-14 23 M. Anand
put()
object_name.put(char_variable);
#include<fstream.h>
#include<string.h>
void main()
{
char str[]=”c++ is better than c”;
ofstream outfile(“test.txt”);
for(int i=0;i<strlen(str);i++)
outfile.put(str[i]);
}
30-Sep-14 24 M. Anand
write() and read() functions
infile.read((char (*)&v,sizeof(v));
outfile.write((char *)&v,sizeof(v));
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
void main()
{
faot height[4]={175.5,153.0,167.25,160.75};
ofstream outfile(“binary”);
outfile.write((char *)&height, sizeof(height));
outfile.close();
30-Sep-14 25 M. Anand
write() and read() functions
for(int i=0;i<4;i++)
{
height[i]=0;
}
ifstream infile(“binaryh”);
infile.read((char *)&height, sizeof(height));
for(int i=0;i<4;i++)
cout<<height[i]<<endl;
}
30-Sep-14 26 M. Anand
Reading and writing a class object
infile.write((char *)&obj, sizeof(obj));
#include<fstream.h>
class person
{
protected:
char name[40];
int age;
Public:
void getdata()
{
cout<<”Enter name\n”;
cin>>name;
cout<<”\nEnter age\n”;
cin>>age;
}
};
30-Sep-14 27 M. Anand
Reading and writing a class object
void main()
{
person pers;
pers.getdata();
ofstream outfile(“person.dat”);
outfile.write((char *)&pers, sizeof(pers));
}
30-Sep-14 28 M. Anand
Reading and writing a class object
infile.read((char *) &obj, sizeof(obj));
#include<fstream.h>
class person
{
protected:
char name[40];
int age;
public:
void showdata()
{
cout<<”Name:=”<<name<<endl;
cout<<”Age:=”<<age;
}
};
30-Sep-14 29 M. Anand
Reading and writing a class object
void main()
{
person pers;
ifstream infile(“person.dat”);
infile.read((char *) &pers,sizeof(pers));
pers.showdata();
}
30-Sep-14 30 M. Anand
Updating a file: Random Access
• Displaying the contents of a file.
• Modifying and existing item.
• Adding a new item.
• Deleting an existing item.
30-Sep-14 31 M. Anand
Updating a file: Random Access
#include<iomanip.h>
class inventory
{
char name[40];
int code;
float cost;
public:
void getdata()
{
cout<<”\nEnter name:”;
cin>>name;
cout<<”\nEnter code”;
cin>>code;
cout<<”\nEnter cost”;
cin>>cost;
}
30-Sep-14 32 M. Anand
Updating a file: Random Access
void putdata()
{
cout<<”\nName:=”<<name;
cout<<”\nCode=”<<code;
cout<<”\nCost:=”<<cost;
}
};
void main()
{
inventory item;
fstream inoutfile;
inoutfile.open(“stock.data”,ios::ate||ios::in||ios::out||ios::binary);
inoutfile.seekg(0,ios::beg);
while(inoutfile.read((char *)& item, sizeof(item))
item.putdata();
inoutfile.clear();
30-Sep-14 33 M. Anand
Updating a file: Random Access
cout<<”Add an item”;
item.getdata();
inoutfile.write((char *)&item, sizeof(item));
cout<<”\nEnter object number to be updated\n”;
int object;
cin>>object;
int locaction=(object-1)*sizeof(item);
inoutfile.seekp(location);
cout<<”\nEnter new values of the object\n”;
item.getdata();
inoutfile.write((char *) &item, sizeof(item));
cout<<”\nContents of updated files\n”;
while(inoutfile.read((char *) &item ,sizeof(item))
{
item.putdata();
30-Sep-14
} 34 M. Anand
}
Error handling during file operations
• A file which we are attempting to open
for reading does not exist.
• The file name used for a new file may
already exist.
• We may attempt an invalid operation
such reading past the EOF.
• We may use an invalid file name.
30-Sep-14 35 M. Anand
Error handling during file operations
Function Return value and meaning
30-Sep-14 36 M. Anand
Error handling during file operations
Example:
……………………
……………………
ifstream infile(‘ABC”);
if (!infile.fail())
{
……………………
……………………
}
else if(infile.eof())
{
……………………
……………………
}
else if(infile.bad())
{
……………………
……………………
30-Sep-14 } 37 M. Anand
Command line arguments
Two forms of main
void main()
void main(int argc, char * argv[])
30-Sep-14 38 M. Anand
Command line arguments
#include<fstream.h>
#include<stdlib.h>
#include<iostream.h>
void main(int argc,char *argv[ ])
{
ifstream in(argv[1]);
ofstream out(argv[2]);
if (argc!=3)
{cout<<"Invalid number of argument...";
exit(0);
}
while(!in.eof())
out.put(in.get());
cout<<"Successfully Copied.....";
in.close();
out.close();
30-Sep-14 } 39 M. Anand
String Stream
• The cout is an ostream object
• The cin is an istream object
String Stream
• istringstream for input
• ostringstream for output
• stringstream
– presented in <sstream> header file
– Inherits both from istringstream and
ostringstream.
Output String Stream
#include<sstream>
#include<iostream>
using namespace std;
int main()
{
ostringstream ostr(“Welcome”);
cout<<ostr.str();
}
Input String Stream
• Input stream object is to read from a
string object
Input String Stream
#include<sstream>
#include<iostream>
using namespace std;
int main()
{
string s=“Welcome”;
istringstream istr(s);
cout<<istr.str();
}
30-Sep-14 45 M. Anand