FOP Lab 6
FOP Lab 6
Lab Report 06
1st SEMESTER
Theory:
File handling:
File handling in C++ is a mechanism to store the output of a program in a file and help perform
various operations on it. Files help store these data permanently on a storage device. Stream:
Ifstream:
An ifstream is an input file stream, i.e. a stream of data used for reading input from a file.
Ofstream:
This data type represents the output file stream and is used to create files and to write
information to files.
End of file:
A special system dependent end-of-file indicator is automatically inserted at the end of every
data file. The eof() function, can be used to detect when this indicator has been reached in a
data file.
Lab work:
1. Write a program in C++ to create a file and write something in file as a
output.
Code:
#include <iostream>
#include <fstream>
using namespace std;
main()
{
string name;
cout<<"Enter your name ";
cin>>name;
ofstream file1("FOPlab6.txt");
file1<<name;
Output:
Code:
#include <iostream>
#include <fstream>
using namespace std;
main()
{
ofstream myfile;
myfile.open("File1.txt");
Output:
Code:
#include <iostream>
#include <fstream>
using namespace std;
main()
{
string name,name1,name2;
ifstream file;
file.open("Foplab6.txt");
file>>name;
file>>name1;
file>>name2;
cout<<name<<endl<<name1<<endl<<name2<<endl;
cout<<"By khurram Rouf 367594";
}
Input File:
Output:
4. Write a program in C++ in which user take input from the file(intro) and
display as a output on screen (use of getline).
Code:
#include <iostream>
#include <fstream>
using namespace std;
main()
{
string data;
ifstream file;
file.open("Foplab6.txt");
Output:
5. Write a program in C++ in which user take input from the file(Lyrics) and
display Lyrics as a output on screen (use of eof).
Code:
#include <iostream>
#include <fstream>
using namespace std;
main()
{
string data;
ifstream file;
file.open("Foplab6.txt");
while(!file.eof())
{
getline(file,data);
cout<<data<<endl;
}
cout<<"By Khurram Rouf 367594";
}
Input File:
Output:
6. Write a program in C++ that reads data pairs from the file sensor.txt and
writes valid data pairs which is positive(+ive).
Code:
#include<iostream>
#include<fstream>
using namespace std;
main()
{
cout<<"By Khurram Rouf 367594"<<endl;
ifstream fin("sensor.txt");
ofstream fout("checkedSensor.txt");
if(fin. fail())
{
cerr << "could not open input file sensor.txt\n";
exit(1);
}
if(fout. fail())
{
cerr << "could not open output file checkedSensor.txt\n";
exit(1);
}
double t, motion;
int count(0);
fin >> t >> motion;
while(!fin.eof())
{
++count;
if(t >= 0 && motion >= 0)
{
fout << t << " " << motion << endl;
}
else
{
cerr << "Bad data encountered on line"
<< count << endl
<< t << " " << motion << endl;
}
fin >> t >> motion;
}
fin.close();
fout.close();
}
Input File:
Output:
Then, a new file will be created where you save the program and sensor file.
Pseudocode:
2. Write a C++ program that reads an html file and filters out all of the tags.
The text from the file, minus the tags, is output to a new file.
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
char character;
bool text_state(true);
string infile, outfile;
ifstream html;
ofstream htmltext;
cout << "enter the name of the input file";
cin >> infile;
cout << "enter the name of the output file";
cin >> outfile;
html.open(infile.c_str());
if(html.fail())
{
cerr << "Error opening input file\n";
exit(1);
}
htmltext.open(outfile.c_str());
html.get(character);
while(!html.eof())
{
if(text_state)
{
if(character == '<')
text_state=false;
else
htmltext << character;
}
else
{
if(character == '>')
text_state = true;
}
html.get(character);
}
html.close();
htmltext.close();
return 0;
}
File Input:
A html chrome file will be created when you save the program.
Output:
After that a file will be created on the name of output.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declare and initialize objects.
int count(0);
double x, y, first, last, sumx(0), sumy(0), sumx2(0),
sumxy(0), denominator, m, b;
string filename;
ifstream zone1;
cout << "Enter name of input file:";
cin >> filename;
zone1.open(filename.c_str());
if(zone1.fail())
{
cerr << "Error opening input file\n";
exit(1);
}
zone1 >> x >> y;
while ( !zone1.eof() )
{
++count;
if (count == 1)
first = x;
sumx += x;
sumy += y;
sumx2 += x*x;
sumxy += x*y;
zone1 >> x >> y;
}
last = x;
// Compute slope and y-intercept.
denominator = sumx*sumx - count*sumx2;
m = (sumx*sumy - count*sumxy)/denominator;
b = (sumx*sumxy - sumx2*sumy)/denominator;
// Set format flags
cout.setf(ios::fixed);
cout.precision(2);
// Print summary information.
cout << "Range of altitudes in km: \n";
cout << first << " to " << last << endl << endl;
cout << "Linear model: \n";
cout << "ozone-mix-ratio = " << m << " altitude + "
<< b << endl;
cout<<"By Khurram Rouf 367594";
// Close file and exit program.
zone1.close();
return 0;
}
File input:
Output:
The End