-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
55 lines (45 loc) · 1.46 KB
/
main.cpp
File metadata and controls
55 lines (45 loc) · 1.46 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
49
50
51
52
53
54
55
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
// Функция № 1: Читает строки из текстового файла и сохраняет их в вектор
void readFromFile(const std::string& filename, std::vector<std::string>& lines) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: " << filename << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
lines.push_back(line);
}
file.close();
}
// Функция № 2: Выводит строки из вектора на экран
void printLines(const std::vector<std::string>& lines) {
for (const auto& line : lines) {
std::cout << line << std::endl;
}
}
// Функция № 3: Записывает строки из вектора в файл
void writeToFile(const std::string& filename, const std::vector<std::string>& lines) {
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Error: " << filename << std::endl;
return;
}
for (const auto& line : lines) {
file << line << std::endl;
}
file.close();
}
int main() {
std::string inputFilename = "input.txt";
std::string outputFilename = "output.txt";
std::vector<std::string> lines;
// Вызов функций
readFromFile(inputFilename, lines);
printLines(lines);
writeToFile(outputFilename, lines);
return 0;
}