Advertisement
Lavig

Другий семестр. Лабораторна робота №20 (Завдання 3)

Jun 4th, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdexcept>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. void readFromFile(const string& filepath) {
  10.     ifstream file(filepath);
  11.     if (!file.is_open()) {
  12.         throw runtime_error("Не вдалося відкрити файл: " + filepath);
  13.     }
  14.     cout << "Вміст файлу \"" << filepath << "\":" << endl;
  15.     string line;
  16.     while (getline(file, line)) {
  17.         cout << line << endl;
  18.     }
  19.     file.close();
  20. }
  21. int main() {
  22.     SetConsoleCP(1251);
  23.     SetConsoleOutputCP(1251);
  24.     string path;
  25.     cout << "Введіть повний шлях до файлу: ";
  26.     getline(cin, path);
  27.     try {
  28.         readFromFile(path);
  29.     }
  30.     catch (const runtime_error& e) {
  31.         cout << "Помилка: " << e.what() << endl;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement