In&out_to file

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

FACULTY OF ELECTRONICS TECHNOLOGY

Subject: CP II Semester 4th


Input &output with files in C++

C++ provides the following classes to perform output and input of characters to/from files:

 Ofstream:Stream class to write on files.


 Ifstream:Stream class to read from files.
 Fstream:Stream class to both read and write from/to files.

Open afile

In order to open a file with a stream object we use its member function open():

Open(filename,mode);

Where filename is a null-terminated character sequence of type const char representing the name of the file to be
opened ,and mode is an following flags:

ios::in Open for input operations.


ios::out Open for output operations.
ios::binary Open in binary mode.
ios::ate Set the initial position at the end of the file.
If this flag is not set to any value,the initial position is the beginning of the file.
ios::app All output operations are performed at the end of the file, appending the content to the current
content of the file.This flag can only be used in streams open for output-only operations.
ios::truck If the file opened for output operations already existed before, its previous content is deleted and
replaced by the new one.
Each one of the open() member functions of the classes ofstream,ifstream and fstream has a default mode that is used
if the file is opened without a second argument:

class Default mode parameter


ofstream ios::out
ifstream ios::in
fstream ios::in ios::out
For ifstream and ofstream classes ,ios::in and ios::out are automatically and respectively assumed,even if amode that
does not include them is passed as second argument to the open() member function.

Closing a file:

At completion of reading from or writing to the streams,you close the streams::

infile.close();

outfile.close();

Example 1:writing to file

We will no create a simple program for writing of data to file.the program will read number
from the user(keyboard)and store them in a file named file.txt
FACULTY OF ELECTRONICS TECHNOLOGY
Subject: CP II Semester 4th
Input &output with files in C++
#include<iostream>
#include<fstream>
using namespace std;
main()
{
int x;
ofstream outfile(“fileone.txt”);
cout<<endl<<”Enter X :”;
cin>>x;
while(x!=0)
{
outfile<<x<<endl;
cout<<endl<<”Enter X , for Exit enter 0”;
cin>>x;
}
outfile.close();
system(“pause”);
return 0;
}

Example :program to write names in text file


#include<iostream>
#include<fstream>
using namespace std;
main()
{
char c[30]=” “;
ofstream outfile(“names.txt”);
cout<<endl<<”Enter name,(only Enter to exit):”;
cin.getline(c,29);
while(c[0]!=”\0”)
{
outfile<<c<<endl;
cout<<endl<<”Enter name:”;
cin.getline(c,29);
}
outfile.close();
system(“pause”);
return 0;
}

Example: this program to write in file the iproddid and price untile iproddid=0
#include<iostream>
#include<fstream>
using namespace std;
FACULTY OF ELECTRONICS TECHNOLOGY
Subject: CP II Semester 4th
Input &output with files in C++
main()
{
int iprodid=1;
double dprice;
ofstream outfile(“d:\prodfile.txt”);
while(iprodid!=0)
{
cout<<endl<<”Enter product id:”;
cin>>iprodid;
cout<<” and price :”;
cin>>dprice;
if(iprodid>0)
outfile<<iprodid<<endl<<dprice<<endl;
}
outfile<<iprodid<<endl<<dprice<<endl;
}
outfile.close();
system(“pause”);
return 0;
}

Example : here complete program to read data from th file prodfile.txt and prints the information on the screen.
#include<iostream>
#include<fstream>
using namespace std;
main()
{
char[30];
ifstream infile(“names.txt”);
while(infile.getline(c,29))
cout<<c<<endl;
infile.close();
system(“pause”);
return 0;
}

Example complete program to read data from the file prodfile.txt


#include<iostream>
#include<fstream>
using namespace std;
main()
{
int iprodId,iSrch,iFound=0;
double dPrice;
FACULTY OF ELECTRONICS TECHNOLOGY
Subject: CP II Semester 4th
Input &output with files in C++
ifstream infil(“d:\prodfile.txt”);
cout<<”Enter product id:”;
cin>>iSrch;
while(infile>>iProdId>>dPrice)
{
if(iProdId==iSrch)
{

cout<<”The price is “<<dPrice;


iFound=1;
break;
}
}
if(!iFound)
cout<<”product missing”;
infile.close();
system(“pause”);
return 0;
}

You might also like