0% found this document useful (0 votes)
32 views17 pages

FOP Lab 6

This document summarizes a programming lab report submitted by Khurram Rouf. The objectives of the lab were to introduce file handling, streams, and ifstream and ofstream in C++. The lab tasks involved writing programs to create and write to files, read from files, and filter HTML tags from a file. Code snippets were provided as examples to open, read, and write to files for each task.

Uploaded by

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

FOP Lab 6

This document summarizes a programming lab report submitted by Khurram Rouf. The objectives of the lab were to introduce file handling, streams, and ifstream and ofstream in C++. The lab tasks involved writing programs to create and write to files, read from files, and filter HTML tags from a file. Code snippets were provided as examples to open, read, and write to files for each task.

Uploaded by

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

Fundamentals of Programming Lab

Lab Report 06

1st SEMESTER

Submitted to: Engr. Ali Hassan


Session: Section: A Group: 2
SUBMITTED BY
Sr. Lab
No. Report
Names CMSID Lab work Lab Tasks Total
(Theor
y)
1 Khurram Rouf 367594

School of Mechanical and Manufacturing Engineering


Objective:
1. Introduction to the file handling
2. Introduction to the stream
3. Introduction to the ifstream and ofstream

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:

A transfer of information in the form of a sequence of a bytes.

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;

cout<<"By Khurram Rouf 367594";


}

Output:

Then a file will be created where you save the program.


2. 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()
{
ofstream myfile;
myfile.open("File1.txt");

myfile<<"This is the line\n";


myfile<<"This is second line";

cout<<"By khurram Rouf 367594";


}

Output:

Then a file will be created where you save the program.


3. Write a program in C++ in which user take input from the file and display as
a output on screen.

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");

for(int i=0; i<=4; i++)


{
getline(file,data);
cout<<data<<endl;
}
cout<<"By Khurram Rouf 367594";
}
Input File:

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.

Then, all the positive pair will be seperated.


Lab Task:
1. Differentiate between pseudocode and Algorithm?
Algorithm:
In general, an algorithm is a description of a procedure that terminates with a result. For
example, the factorial of a number x is x multiplied by x-1 multiplied by x-2 and so on until it is
multiplied by 1. The factorial of 6 is 6! = 6 x 5 x 4 x 3 x 2 x 1=720. This is an algorithm that
follows a set procedure and terminates in a result.

Pseudocode:

Pseudocode is an informal high-level description of a computer program or algorithm. It


is written in symbolic code which must be translated into a programming language before it can
be executed.
Example

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.

Now, this is the final 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

You might also like