219 Assignment #4
219 Assignment #4
ASSIGNMENT # 4
CSC241-OBJECT ORIENTED PROGRAMMING (OOP)
SUBMITTED BY:
DANISH RIASAT
SUBMITTED TO:
MR. ALI USMAN
REGISTRATION NO.
FA22-BCS-219
Question #4
public:
Output
Chapter 12
Question #1
#include <iostream> // open it for append
#include <fstream> // for file streams file.open("DIST.DAT", ios::binary |
using namespace std; ios::app | ios::out | ios::in );
class Distance { // English Distance class do{ // data from user to file
private: cout << "\nDistance";
int feet; dist.getdist(); // get a distance
float inches; // write to file
public: file.write( (char*)&dist,
Distance() : feet(0), sizeof(dist) );
inches(0.0) // constructor (no args) cout << "Enter another distance
{} (y/n)? ";
Distance(int ft, float in) : cin >> ch;
feet(ft), inches(in) // constructor (two args) }
{} while(ch=='y'); // quit on ‘n’
void getdist(){ // get length file.seekg(0); // reset to start of file
from user // read first distance
cout << "\n Enter feet: file.read( (char*)&dist, sizeof(dist) );
"; int count = 0;
cin >> feet; while( !file.eof() ){ // quit on EOF
cout << " Enter inches: cout << "\nDistance " << +
"; +count << ": "; // display dist
cin >> inches; dist.showdist();
} file.read( (char*)&dist,
void showdist(){ // display sizeof(dist) ); // read another distance
distance }
cout << feet << "\'-" << cout << endl;
inches << "''"; return 0;
} }
};
int main(){
char ch;
Distance dist; // create a Distance object
fstream file; // create input/output file
Output
Question #2
#include <iostream> while (getline(sourceFile, line)) {
#include <fstream> if (!(destFile << line << endl)) {
#include <string> cerr << "Error: Cannot write to
using namespace std; destination file '" << destinationFileName << "'"
int main() { << endl;
// Source and destination file names sourceFile.close();
const char* sourceFileName = "dan.txt"; destFile.close();
const char* destinationFileName = "f.txt"; return 1; // Exit with an error code
// Open the source file }
ifstream sourceFile(sourceFileName); }
if (!sourceFile.is_open()) {
cerr << "Error: Cannot open source file '" // Close the files
<< sourceFileName << "'" << endl; sourceFile.close();
return 1; // Exit with an error code destFile.close();
}
cout << "File copy successful." << endl;
// Open the destination file
ofstream destFile(destinationFileName); // Wait for user input
if (!destFile.is_open()) { cout << "Press Enter to exit...";
cerr << "Error: Cannot open destination cin.get();
file '" << destinationFileName << "'" << endl;
sourceFile.close(); // Close the source file return 0; // Exit successfully
return 1; // Exit with an error code }
}
Output
Before Copying
After Copying
Question #3
#include <iostream>
#include <fstream>
using namespace std;
int main() {
const char* filename = "Untitled3.cpp";
ifstream file(filename, ios::binary | ios::ate);
if (!file.is_open()) {
cerr << "Error opening file: " << filename << :endl;
return 1;
}
streamsize size = file.tellg();
cout << "Size of file " << filename << ": " << size << " bytes" << endl;
file.close();
return 0;
}
Output
Question #4
#include <iostream>
#include <fstream> outFile.close(); // Close the ofstream object
#include <string>
using namespace std; // Open file for reading
int main() { ifstream inFile("employee_data.txt");
ofstream outFile("employee_data.txt",
ios::app); // Open file for appending if (!inFile) {
cerr << "Error opening file for reading." <<
if (!outFile) { endl;
cerr << "Error opening file for writing." << return 1;
endl; }
return 1;
} // Read and display all data from the file
string line;
char choice; cout << "\nEmployee Data from File:\n";
do { while (getline(inFile, line)) {
string firstName, middleInitial, lastName; cout << line << std::endl;
unsigned long employeeNumber; }