Вы наверное поняли?
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::string userInput;
std::ofstream file;
while (true) {
std::cout << "> ";
std::getline(std::cin, userInput);
if (userInput == "exit") {
break;
}
// Process command
if (userInput.find("touch") != std::string::npos) {
std::string filePath = userInput.substr(6);
if (file.is_open()) {
file.open(filePath, std::ios::out | std::ios::trunc);
file.close();
std::cout << "Created: " << filePath << "\n";
} else {
std::cout << "Could not create file: " << filePath << "\n";
}
} else if (userInput.find("open") != std::string::npos) {
std::string filePath = userInput.substr(5);
if (file.is_open()) {
file.open(filePath, std::ios::in);
if (file.is_open()) {
std::cout << "Opened: " << filePath << "\n";
std::cout << "Contents:\n";
while (file) {
std::string line;
std::getline(file, line);
if (!file.eof()) {
std::cout << line << "\n";
}
}
std::cout << "\n";
file.close();
} else {
std::cout << "Could not open: " << filePath << "\n";
}
}
}
}
}