Unit-7_IO and File Management
Unit-7_IO and File Management
Unit-7
I/O and File
Management
Dr. Pawan Kumar Singh, Department of Information Technology
+91-6291555693
[email protected] / [email protected]
I/O and File Management
Concept of streams
cin and cout objects
C++ stream classes
Unformatted and formatted I/O
Manipulators
File stream
C++ File stream classes
File management functions
File modes
Binary and random Files
Concepts of Streams
Concept of Streams
Program Program
Input stream
Input
device
Program
Output stream
Output
device
Stream class for console I/O operations
General input/output
input stream ios stream class
class
pointer
output stream class
istream streambuf ostream
input output
Example: output:
output:
cout.precision(6);
cout.fill('*');
cout.width(6);
cout.setf(ios::left,ios::adjustfield);
* * * 5 42 3. 6 4 5 7 5
cout.width(10);
cout.width(6);
cout<<"543";
cout.width(6);
cout<<sqrt(7);
cout<<"543"; output:
cout.fill('#');
cout<<"543"; 5 4 3 # # #
Flags and bit fields
Format required Flag (arg1) Bit-field (arg2)
Left justified output ios::left ios::adjustfield
Right justified output ios::right ios::adjustfield
Scientific notation ios::scientific ios::floatfield
Fixed point notation ios::fixed ios::floatfield
Decimal base ios::dec ios::basefield
Octal base ios::oct ios::basefield
Hexadecimal base ios::hex ios::basefield
setf(arg1, arg2)
arg-1: one of the formatting flags.
arg-2: bit field specifies the group to which the formatting flag belongs.
Manipulators for formatted I/O operations
Manipulators are special functions that can be included in the I/O
statements to alter the format parameters of a stream.
To access manipulators, the file <iomanip> should be included in
the program.
Function Manipulator Meaning
width() setw() Set the field width.
precision() setprecision() Set the floating point precision.
fill() setfill() Set the fill character.
setf() setiosflags() Set the format flag.
unsetf() resetiosflags() Clear the flag specified.
“\n” endl Insert a new line and flush stream.
File stream classes
File input output streams
read data
Input stream
data
input
iostream
filebuf Its purpose is to set the file buffers to read and write.
File handling steps
1. Open / Create a file
2. Read / Write a file
3. Close file
Create and Write File (Output)
Create object of ofstream class
ofstream send;
ofstream send;
3 send.open("abc.txt",ios::out); //open()
function with mode
File opening modes
Parameter Meaning
ios :: in Open file for reading only
ios :: out Open file for writing only
ios :: app Append to end-of-file
ios :: ate Go to end-of-file on opening
ios :: binary Binary file
ios :: trunc Delete content of file if exists
ios :: nocreate Open fails if the file does not exists
ios :: noreplace Open fails if the file already exists
File operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open("example.txt",ios::out);
myfile << "This is India.\n";
myfile.close();
}
example.txt
cout<<line;
rfile.close();
}
example.txt
This is India
.cpp This is india
int main()
{ File operations program
char product[20];
int price;
cout<<"Enter product name=";
cin>>product;
cout<<"Enter price=";
cin>>price;
Opening a file to write
ofstream outfile("stock.txt");
outfile<<product<<endl; data into file
outfile<<price;
cout<<product<<endl;
cout<<price;
}
File handling Program
Write a program that opens two text files for reading data.
It creates a third file that contains the text of first file and then
that of second file
(text of second file to be appended after text of the first file, to
produce the third file).
int main() {
fstream file1,file2,file3;
file1.open("one.txt",ios::in);
file2.open("two.txt",ios::in);
file3.open("three.txt",ios::app);
char ch1,ch2;
while(!file1.eof())
{
file1.get(ch1); cout<<ch1<<endl;
file3.put(ch1);
}
file1.close();
while(!file2.eof())
{
file2.get(ch2); cout<<ch2<<endl;
file3.put(ch2);
}
file2.close(); file3.close();
}
File pointers
Each file has two associated pointers known as the file pointers.
One of them is called input pointer (or get pointer) and the other
is called output pointer (or put pointer).
Input pointer is used for reading the content of a given file
location.
Output pointer is used for writing to a given file location.
Functions for manipulation of file pointers
Function Meaning
seekg() Moves get pointer (input) to specified location
seekp() Moves put pointer (output) to specified location
tellg() Gives current position of the get pointer
tellp() Gives current position of the put pointer
ifstream rcv;
ofstream send;
Function Meaning
ios::beg offset counted from the beginning of the stream
ios::cur offset counted from the current position of the
stream pointer
ios::end offset counted from the end of the stream
write() and read() functions
The functions write() and read(), different from the
functions put() and get(), handle the data in binary form.
infile.read ((char * ) &V,sizeof(V));
outfile.write ((char *) &V ,sizeof(V));
These functions take two arguments. The first is the address of the
variable V, and the second is the length of that variable in bytes.
The address of the variable must be cast to type char*(i.e pointer
to character type).
Reading & Writing class objects
class inventory
{
char name[10];
float cost;
public:
void readdata()
{
cout<<"Enter Name=";
cin>>name;
cout<<"Enter cost=";
cin>>cost;
}
void displaydata()
{
cout<<"Name="<<name<<endl;
cout<<"Cost="<<cost;
}
};
Reading & Writing class objects
int main()
{
inventory ob1;
cout<<"Enter details of product\n";
fstream file;
file.open("stock.txt",ios::in | ios::app);
ob1.readdata();
file.write((char *)&ob1,sizeof(ob1));
file.read((char *)&ob1,sizeof(ob1));
ob1.displaydata();
file.close();
}
Thank You