-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (41 loc) · 1.26 KB
/
Copy pathmain.cpp
File metadata and controls
48 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string fileA = "Employee.csv";
std::string fileB = "Promotion.csv";
// Check if Employee.csv exists
if (!fs::exists(fileA)) {
std::cerr << "Error: " << fileA << " does not exist.\n";
return 1;
}
// Check if Employee.csv is empty
if (fs::file_size(fileA) == 0) {
std::cerr << "Error: " << fileA << " is empty.\n";
return 1;
}
// Open Employee.csv for reading
std::ifstream inFile(fileA);
if (!inFile) {
std::cerr << "Error: Cannot open " << fileA << " for reading.\n";
return 1;
}
// Open Promotion.csv for writing (this will create it if not exists, or empty it if exists)
std::ofstream outFile(fileB, std::ios::trunc);
if (!outFile) {
std::cerr << "Error: cannot write to " << fileB << "\n";
return 1;
}
// Copy content from Employee.csv to Promotion.csv
std::string line;
while (std::getline(inFile, line)) {
outFile << line << "\n";
}
inFile.close();
outFile.close();
// Output Promotion.csv content
std::cout << "Successfully copied to " << fileB << ":\n";
return 0;
}