0% found this document useful (0 votes)
56 views20 pages

Assignment 2: File Handling

The document discusses file handling in C++. It includes programs to create classes to store and display bill information, write and read to files, handle command line arguments to write numbers to different files, accept a file name from the user and write inputted text to that file, and write and read to different files to store country and capital names.

Uploaded by

Bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views20 pages

Assignment 2: File Handling

The document discusses file handling in C++. It includes programs to create classes to store and display bill information, write and read to files, handle command line arguments to write numbers to different files, accept a file name from the user and write inputted text to that file, and write and read to different files to store country and capital names.

Uploaded by

Bhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Assignment 2

FILE HANDLING
1. Write a program in C++ which contain the class bill
which accept & displays the class members name, age,
bill number, bill amount

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
class bill
{
public:
int iBill_no;
float fBill_amt;
char name[7];
char age[2];
void getdata()
{
cout << "enter your Name:" ;
cin >>name ;
cout <<"Enter your age";
cin >>age;
cout << "Enter Bill Number:";
cin >> iBill_no;
cout << "Enter Bill Amount:";
cin >> fBill_amt;
}
void showdata()
{ cout << "Name : " <<name<<endl;
cout << "Age : " <<age<<endl;
cout << "Bill Number is: " << iBill_no <<endl;
cout << "Bill Amount: " <<fBill_amt << endl;
  } 
};
 
void main()
{
clrscr();
bill billobj;
char rep;
ofstream ofil("trial.dat",ios::in|ios::out);
bill Bvar;
do
{
Bvar.getdata();
ofil.write((char*)&Bvar,sizeof(Bvar));
cout << "Do you want to continue? (y/n)";
cin >> rep;
}while(rep!='n');
 
ofil.close();
ifstream ifile("trial.dat",ios::in|ios::out);
ifile.seekg(0);
//ifile.read((char *)&billobj,sizeof(billobj));
while(ifile)
{
cout << "output :\n";
ifile.read((char*)&billobj,sizeof(billobj));
billobj.showdata();
}
getch();
}
INPUT:
enter your Name:sai
Enter your age21
Enter Bill Number:111
Enter Bill Amount:200000
Do you want to continue? (y/n)y
enter your Name:dalcomp
Enter your age21
Enter Bill Number:112
Enter Bill Amount:300000
Do you want to continue? (y/n)y
enter your Name: samarth
Enter your age21
Enter Bill Number:113
Enter Bill Amount:400000
Do you want to continue? (y/n)n
OUTPUT IN ‘TRIAL.DAT’ FILE :
Name : sai
Age : 21
Bill Number is: 111
Bill Amount: 200000
Name : dalcomp
Age : 21
Bill Number is: 112
Bill Amount: 300000
Name : samarth
Age : 21
Bill Number is: 113
Bill Amount: 400000
2. Write a program in the C++ which contains the file
& which accepts & displays the contents of the file

#include <fstream.h>
int main()
{
char fileName[80];
char buffer[255]; // for user input
cout << "File name: ";
cin >> fileName;

ofstream fout(fileName); // open for writing


fout << "This line written directly to the file...\n";
cout << "Enter text for the file: ";

 
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255); // get the user's input
fout << buffer << "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen
 
ifstream fin(fileName); // reopen for reading
cout << "Here's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;
 
cout << "\n***End of file contents.***\n";
 
fin.close(); // always pays to be tidy
return 0;
}
3. Write a program in the C++ which accepts the
command from the user using the two command
line argument

#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
int main(int argc ,char *argv[])
{
int number[4]={11,22,33,44};
if(argc!=3)
{
cout<<"argc= "<<argc<<endl;
exit(1);
}
ofstream fout1,fout2;
fout1.open(argv[1]);
if(fout1.fail())
{ cout<<"could not open the file";
exit(1);
}
fout2.open(argv[2]);
if(fout2.fail())
{ cout<<"could not open file";
exit(1);
}
for(int i=0;i<4;i++)
{
if(number[i]%2==0)
fout2<<number[i]<<" ";
else
fout1<<number[i]<<" ";
}
fout1.close();
fout2.close();
ifstream fin; char ch;
for(i=0;i<argc;i++)
{
fin.open(argv[i]);
cout<<"contents of file"<<argv[i]<<endl;
do
{
fin.get(ch);
cout<<ch;
}
while(fin);
cout<<endl;
fin.close();
}
return 0; 
}
OUTPUT:

filetest1.txt
11 33
filetest2.txt
22 44
4. Write a program in the C++ which accepts the file
from the user & write the contents into the file.

#include <fstream.h>
#include<conio.h>
int main()
{
char fileName[80];
char buffer[255]; // for user input
clrscr();
cout << "File name: ";
cin >> fileName;
 
ofstream fout(fileName); // open for writing
fout << "This line written directly to the file...\n";
cout << "Enter text for the file: ";
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255); // get the user's input
fout << buffer << "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen

ifstream fin(fileName); // reopen for reading


cout << "Here's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;
 
cout << "\n***End of file contents.***\n";
 
fin.close(); // always pays to be tidy
return 0;
getch();
}
OUTPUT:

File name: dalcomp4.txt


Enter text for the file: This line written directly to the file...
This is file handling
Here's the contents of the file:
This line written directly to the file...
This is file handling
***End of file contents.***
5. Write a program in the C++ which will accept the
file from the user & which will be used for writing
the data into file and reading data from that file

#include<iostream.h>
#include<fstream.h>
void main()
{
const int N=80;
char line[N];
ofstream fout;
fout.open("country.doc");
fout<<"United States of American\n";
fout<<"United Kingdom\n";
fout<<"South Korea\n";
fout.close();
fout.open("capital");
fout<<"Washington\n";
fout<<"London\n";
fout<<"Seoul\n";
fout.close();

ifstream fin;
fin.open("country.doc");
cout<<"contents of country file\n";
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;;
}
fin.close();
fin.open("capital");
cout<<"\ncontents of capital file\n";
while(fin)
{
fin.getline(line,N);
cout<<line<<endl;;
}
fin.close();
}

fin.open(fileName); // reassign existing fin object!


if (!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
return(1);
}
cout << "\nHere's the contents of the file:\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << "\n***End of file contents.***\n";
fin.close();
return 0;
}
OUTPUT:

Please re-enter the file name: dalcomp_append.txt


Opening dalcomp_append.txt in append mode...
 
Enter text for the file: YOU ARE LEARNING FILE HANDLING!
 
Here's the contents of the file:
Hiii welcome to C++
 
YOU ARE LEARNING FILE HANDLING!
 
***End of file contents.***
 
 
 
 
 

You might also like