Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <complex>
- #include <string>
- #include <tuple>
- #include <optional>
- #include <vector>
- #include <experimental/filesystem>
- #include <fstream>
- // FILE TASK
- std::string getData() {
- std::string mainData{};
- std::string subData{};
- while (subData != "koniec") {
- std::cout << "Enter data: " << '\n';
- std::getline(std::cin, subData);
- std::cout << "\nYou entered: " << subData << '\n';
- if (subData != "koniec") mainData += " " + subData;
- }
- std::cout << "\nAll entered data: " << mainData;
- std::cout << "\nEnd data entering!\n";
- return mainData;
- }
- namespace fs = std::experimental::filesystem;
- void fileAdder(const std::string& fname) {
- if (!fs::exists(fs::path(fname))) {
- std::ofstream file(fname);
- if (file.is_open()) {
- auto text = getData();
- file << text;
- file.close();
- }
- else {
- std::cerr << "File error!";
- }
- }
- else {
- std::cout << "File exist!";
- }
- }
- void showFileData(const std::string& fname) {
- if (!fs::exists(fs::path(fname))) {
- std::cout << "File not exist! Nothing to show!";
- return;
- }
- std::ifstream readFile(fname);
- std::string outData;
- std::cout << "\nFILE INCLUDED:\n";
- if (readFile.is_open()) {
- while (!readFile.eof()) {
- readFile >> outData;
- std::cout << outData << "\n";
- }
- }
- }
- int main() {
- std::string fname;
- std::cout<<"Entry filename to save: ";
- std::getline(std::cin, fname);
- fileAdder(fname);
- showFileData(fname);
- auto data = getData();
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement