Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <complex>
  3. #include <string>
  4. #include <tuple>
  5. #include <optional>
  6. #include <vector>
  7. #include <experimental/filesystem>
  8. #include <fstream>
  9.  
  10. // FILE TASK
  11.  
  12.  
  13. std::string getData() {
  14. std::string mainData{};
  15.  
  16. std::string subData{};
  17.  
  18. while (subData != "koniec") {
  19. std::cout << "Enter data: " << '\n';
  20. std::getline(std::cin, subData);
  21. std::cout << "\nYou entered: " << subData << '\n';
  22.  
  23. if (subData != "koniec") mainData += " " + subData;
  24. }
  25. std::cout << "\nAll entered data: " << mainData;
  26. std::cout << "\nEnd data entering!\n";
  27.  
  28. return mainData;
  29. }
  30.  
  31. namespace fs = std::experimental::filesystem;
  32.  
  33. void fileAdder(const std::string& fname) {
  34.  
  35. if (!fs::exists(fs::path(fname))) {
  36.  
  37. std::ofstream file(fname);
  38.  
  39. if (file.is_open()) {
  40. auto text = getData();
  41.  
  42. file << text;
  43.  
  44. file.close();
  45. }
  46. else {
  47. std::cerr << "File error!";
  48. }
  49. }
  50. else {
  51. std::cout << "File exist!";
  52. }
  53. }
  54.  
  55. void showFileData(const std::string& fname) {
  56. if (!fs::exists(fs::path(fname))) {
  57. std::cout << "File not exist! Nothing to show!";
  58. return;
  59. }
  60.  
  61. std::ifstream readFile(fname);
  62.  
  63. std::string outData;
  64. std::cout << "\nFILE INCLUDED:\n";
  65. if (readFile.is_open()) {
  66. while (!readFile.eof()) {
  67. readFile >> outData;
  68. std::cout << outData << "\n";
  69. }
  70. }
  71.  
  72. }
  73.  
  74. int main() {
  75.  
  76. std::string fname;
  77.  
  78. std::cout<<"Entry filename to save: ";
  79. std::getline(std::cin, fname);
  80.  
  81. fileAdder(fname);
  82.  
  83. showFileData(fname);
  84.  
  85. auto data = getData();
  86.  
  87. std::cin.get();
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement