-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRead_Write_TXT.cpp
56 lines (43 loc) · 1.46 KB
/
Read_Write_TXT.cpp
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
49
50
51
52
53
54
55
56
// CSC252 Programming in C++
// Mark Langston 6/8/2024
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Program intro
std::cout << "CSC252 - Reading and Writing Text Files" << std::endl;
std::string inputFilePath;
std::string outputFilePath;
// Prompts user for file path of text file
std::cout << "Enter the path of the input file: ";
std::getline(std::cin, inputFilePath);
std::ifstream inputFile(inputFilePath);
// Prompts user for where to save the text file
std::cout << "Enter the path for the output file: ";
std::getline(std::cin, outputFilePath);
std::ofstream outputFile(outputFilePath);
std::string line;
// Loops through each line of the inputFile
while (std::getline(inputFile, line)) {
if (!line.empty()) {
outputFile << line << std::endl;
}
}
// Closes the files
inputFile.close();
outputFile.close();
// Function that displays the files contents
auto displayFileContent = [](const std::string& fileName) {
std::ifstream file(fileName);
std::string line;
std::cout << fileName << ":" << std::endl;
while (std::getline(file, line)) {
std::cout << line << std::endl;
}
std::cout << "-------------------" << std::endl;
};
// Display contents of input and output files
displayFileContent(inputFilePath);
displayFileContent(outputFilePath);
return 0;
}