FileHandling Assignment.1
FileHandling Assignment.1
Objective:
To demonstrate how file reading and writing can be implemented in C++ using constructors with separate
Key Points:
Main Idea:
Each class is responsible for a single task: FileWriter writes data to a file, while FileReader reads data from a
file.
Conclusion:
The separate way offers better organization and code modularity, which is useful in larger projects.
Code:
#include <iostream>
#include <fstream>
#include <string>
public:
ofstream outfile(filename);
if (outfile.is_open()) {
outfile.close();
} else {
};
// Class for reading from a file
class FileReader {
public:
ifstream infile(filename);
string line;
if (infile.is_open()) {
infile.close();
} else {
};
int main() {
FileReader reader("sample.txt");
return 0;
Outcome:
PART 2
Objective:
To implement file writing and reading using a single constructor in one class to perform both tasks
sequentially.
Key Points:
- Only one class is used: FileHandler.
Main Idea:
The constructor of a single class handles both writing to and reading from a file.
Conclusion:
This method is ideal for quick file operations where separation of logic is not necessary.
Code:
#include <iostream>
#include <fstream>
#include <string>
class FileHandler
{ public:
return;
}
ifstream infile(filename);
} infile.close(); } else
n";
};
int main() {
return 0;
Outcome:
Writing completed.