0% found this document useful (0 votes)
2 views7 pages

Lab 12 CPP

Uploaded by

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

Lab 12 CPP

Uploaded by

wahabiyi123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer and Software Engineering

College of E&ME, NUST, Rawalpindi

LAB # 12: File Handling in C++


CLO-4/PLO-5

Course Instructor:

Lab Instructor: Engr. Ayesha Khanam

Sr. No. Student’s Name CMS ID

1 Abdul wahab 544532

1
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

LAB # 12: File Handling in C++

Software Requirement(s): MS Visual Studio

Lab Tasks

1. Write a C++ program to write the following contents in the file and then display the file. The
delimiter is $.
Contents of [Link]:

Jayne Murphy$47 Jones Circle$Almond, NC 28702\n$Bobbie Smith$ 217


Halifax Drive$Canton, NC 28716\n$Bill Hammet$PO Box 121$ Springfield,
NC 28357\n$zz

#include<iostream>
#include<fstream>
using namespace std;

int main(){
ofstream out("[Link]");
out<<"Jayne Murphy$47 Jones Circle$Almond, NC 28702\n$";
out<<"Bobbie Smith$217 Halifax Drive$Canton, NC 28716\n$";
out<<"Bill Hammet$PO Box 121$Springfield, NC 28357\n$";
[Link]();

ifstream in("[Link]");
string line;
while(getline(in,line)){
cout<<line<<endl;
}
[Link]();
return 0;
}

2
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

2. Write C++ Program to Store Information of a 2Student in a file. It stores the information
(name,roll and marks entered by the user) of a student in a file and displays it on the screen. Use
store and display functions in program.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct Student {
string name;
int roll;
float marks;
};

void storeData(Student s) {
ofstream outFile("[Link]", ios::app);
if (outFile.is_open()) {
outFile << [Link] << " " << [Link] << " " << [Link] << endl;
[Link]();
}
}

void displayData() {
ifstream inFile("[Link]");
string name;
int roll;
float marks;

if (inFile.is_open()) {
while (inFile >> name >> roll >> marks) {
cout << "Name: " << name << " | Roll: " << roll << " | Marks: " << marks << endl;
}
[Link]();
}
}

int main() {
Student s;
cout << "Name: ";
cin >> [Link];
cout << "Roll: ";
cin >> [Link];
cout << "Marks: ";
cin >> [Link];

storeData(s);
displayData();

3
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

return 0;

1. Write a program that uses a file to store the following weather data for a particular month:
● Total Rainfall
● High Temperature
● Low Temperature
● Average Temperature
The program should hold weather data for an entire year in a file. When the program runs, it
should ask the user to enter data for each month. (The average temperature should be calculated.)
Once the data are entered for all the months, the program should calculate and display the average
monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year
(and the months they occurred in), and the average of all the monthly average temperatures. Store
this data in file as well.

#include <iostream>

#include <fstream>

#include <string>

#include <iomanip>

using namespace std;

struct Weather {

4
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

string month;

double rain, high, low, avg;

};

int main() {

Weather year[12];

string names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec" };

double totalRain = 0, avgSum = 0;

int hIdx = 0, lIdx = 0;

ofstream outFile("[Link]");

for (int i = 0; i < 12; i++) {

year[i].month = names[i];

cout << names[i] << " - Rain: ";

cin >> year[i].rain;

cout << names[i] << " - High: ";

cin >> year[i].high;

cout << names[i] << " - Low: ";

cin >> year[i].low;

year[i].avg = (year[i].high + year[i].low) / 2.0;

totalRain += year[i].rain;

avgSum += year[i].avg;

5
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

if (year[i].high > year[hIdx].high) hIdx = i;

if (year[i].low < year[lIdx].low) lIdx = i;

outFile << year[i].month << " " << year[i].rain << " " << year[i].high
<< " " << year[i].low << " " << year[i].avg << endl;

double monthlyAvgRain = totalRain / 12.0;

double yearlyAvg = avgSum / 12.0;

cout << fixed << setprecision(2);

cout << "\nTotal Rain: " << totalRain << endl;

cout << "Avg Monthly Rain: " << monthlyAvgRain << endl;

cout << "High: " << year[hIdx].high << " (" << names[hIdx] << ")" <<
endl;

cout << "Low: " << year[lIdx].low << " (" << names[lIdx] << ")" << endl;

cout << "Yearly Avg: " << yearlyAvg << endl;

outFile << totalRain << " " << monthlyAvgRain << " " << year[hIdx].high
<< " " << year[lIdx].low << " " << yearlyAvg << endl;

[Link]();

return 0;

6
Department of Computer and Software Engineering
College of E&ME, NUST, Rawalpindi

You might also like