cpp
exercises
exercises.cpp⚙️cpp
/**
* File Handling - Exercises
* Compile: g++ -std=c++17 -Wall exercises.cpp -o exercises
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// ============================================================
// Exercise 1: Line Counter ⭐
// ============================================================
// TODO: Count lines, words, and characters in a file
void exercise1() {
// int lines, words, chars;
// countFile("file.txt", lines, words, chars);
// cout << "Lines: " << lines << ", Words: " << words << ", Chars: " << chars;
cout << "(Implement file counter)" << endl;
}
// ============================================================
// Exercise 2: File Copy ⭐
// ============================================================
// TODO: Copy contents from source to destination file
void exercise2() {
// bool success = copyFile("source.txt", "dest.txt");
cout << "(Implement file copy)" << endl;
}
// ============================================================
// Exercise 3: Search in File ⭐⭐
// ============================================================
// TODO: Search for a string in file, return line numbers
void exercise3() {
// vector<int> lines = searchInFile("file.txt", "pattern");
// Pattern found on lines: 5, 12, 23
cout << "(Implement file search)" << endl;
}
// ============================================================
// Exercise 4: CSV Parser ⭐⭐
// ============================================================
// TODO: Parse CSV file into vector of vectors
void exercise4() {
// auto data = parseCSV("data.csv");
// Returns 2D vector of strings
cout << "(Implement CSV parser)" << endl;
}
// ============================================================
// Exercise 5: Log Rotator ⭐⭐
// ============================================================
// TODO: If log file > maxSize, rename to .old and create new
void exercise5() {
// rotateLog("app.log", 1024); // Max 1KB
cout << "(Implement log rotation)" << endl;
}
// ============================================================
// Exercise 6: Binary Record Manager ⭐⭐⭐
// ============================================================
// TODO: Create class to manage binary records
// - add(record)
// - get(id) -> record
// - update(id, record)
// - remove(id)
void exercise6() {
// RecordManager rm("records.bin");
// rm.add({1, "Alice", 95.5});
// auto r = rm.get(1);
cout << "(Implement binary record manager)" << endl;
}
// ============================================================
// Exercise 7: File Diff ⭐⭐⭐
// ============================================================
// TODO: Compare two files, output different lines
void exercise7() {
// vector<pair<int, string>> diffs = fileDiff("file1.txt", "file2.txt");
// Line 5 differs: "old text" vs "new text"
cout << "(Implement file diff)" << endl;
}
// ============================================================
// Exercise 8: Config File Parser ⭐⭐⭐
// ============================================================
// TODO: Parse key=value config file into map
// Support comments (#) and sections [section]
void exercise8() {
// auto config = parseConfig("app.conf");
// config["database.host"] = "localhost"
cout << "(Implement config parser)" << endl;
}
// ============================================================
// MAIN
// ============================================================
int main() {
cout << "=== File Handling Exercises ===" << endl;
cout << "\nEx1: Line Counter" << endl;
exercise1();
cout << "\nEx2: File Copy" << endl;
exercise2();
cout << "\nEx3: Search in File" << endl;
exercise3();
cout << "\nEx4: CSV Parser" << endl;
exercise4();
cout << "\nEx5: Log Rotator" << endl;
exercise5();
cout << "\nEx6: Binary Records" << endl;
exercise6();
cout << "\nEx7: File Diff" << endl;
exercise7();
cout << "\nEx8: Config Parser" << endl;
exercise8();
return 0;
}
// ============================================================
// ANSWERS
// ============================================================
/*
Ex1:
void countFile(const string& filename, int& lines, int& words, int& chars) {
ifstream file(filename);
lines = words = chars = 0;
string line;
while (getline(file, line)) {
lines++;
chars += line.length();
stringstream ss(line);
string word;
while (ss >> word) words++;
}
}
Ex2:
bool copyFile(const string& src, const string& dest) {
ifstream in(src, ios::binary);
ofstream out(dest, ios::binary);
if (!in || !out) return false;
out << in.rdbuf();
return true;
}
Ex3:
vector<int> searchInFile(const string& filename, const string& pattern) {
vector<int> results;
ifstream file(filename);
string line;
int lineNum = 0;
while (getline(file, line)) {
lineNum++;
if (line.find(pattern) != string::npos) {
results.push_back(lineNum);
}
}
return results;
}
Ex4:
vector<vector<string>> parseCSV(const string& filename) {
vector<vector<string>> data;
ifstream file(filename);
string line;
while (getline(file, line)) {
vector<string> row;
stringstream ss(line);
string cell;
while (getline(ss, cell, ',')) {
row.push_back(cell);
}
data.push_back(row);
}
return data;
}
Ex8:
map<string, string> parseConfig(const string& filename) {
map<string, string> config;
ifstream file(filename);
string line, section;
while (getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
if (line[0] == '[') {
section = line.substr(1, line.find(']') - 1) + ".";
} else {
auto pos = line.find('=');
if (pos != string::npos) {
string key = section + line.substr(0, pos);
string value = line.substr(pos + 1);
config[key] = value;
}
}
}
return config;
}
*/