Advertisement
believe_me

Untitled

Oct 31st, 2021
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #pragma once
  2. bool CheckExtension(const std::string& path_to_file) {
  3.     const std::string extension = "txt";
  4.     if (path_to_file.substr(path_to_file.length() - 3) == extension)
  5.     {
  6.         return true;
  7.     }
  8.     else {
  9.         std::cout << "Wrong file's extension!\n";
  10.         return false;
  11.     }
  12. }
  13.  
  14. bool CheckPermissionForReading(const std::string& path_to_file)
  15. {
  16.     std::ifstream file_out(path_to_file);
  17.     if (file_out.is_open())
  18.     {
  19.         file_out.close();
  20.         return true;
  21.     }
  22.     else
  23.     {
  24.         std::cout << "File can not be opened!\n";
  25.         return false;
  26.     }
  27. }
  28.  
  29. bool CheckPermissionForWriting(const std::string& path_to_file)
  30. {
  31.     std::ofstream file_out(path_to_file);
  32.     if (file_in.is_open())
  33.     {
  34.         file_in.close();
  35.         return true;
  36.     }
  37.     else
  38.     {
  39.         std::cout << "Can not write in this file!\n";
  40.         return false;
  41.     }
  42. }
  43.  
  44.  
  45.  
  46. const std::string inputPathToFileForWriting()
  47. {
  48.     std::string path_to_file;
  49.     do
  50.     {
  51.         std::cout << "Enter path to file: \n";
  52.         getline(std::cin, path_to_file);
  53.     } while (!CheckExtension(path_to_file) || !CheckPermissionForWriting(path_to_file));
  54.     return path_to_file;
  55. }
  56.  
  57. const std::string inputPathToFileForReading()
  58. {
  59.     std::string path_to_file;
  60.     do
  61.     {
  62.         std::cout << "Enter path to file: \n";
  63.         getline(std::cin, path_to_file);
  64.     } while (!CheckExtension(path_to_file) || !CheckPermissionForReading(path_to_file));
  65.     return path_to_file;
  66. }
  67.  
  68. const std::string inputPathToFile()
  69. {
  70.     std::string path_to_file;
  71.     do
  72.     {
  73.         std::cout << "Enter path to file: \n";
  74.         getline(std::cin, path_to_file);
  75.     } while (!CheckExtension(path_to_file) || !CheckPermissionForReading(path_to_file) || !CheckPermissionForWriting(path_to_file));
  76.     return path_to_file;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement