diff --git a/README.md b/README.md index d17f05c..6fb6347 100644 --- a/README.md +++ b/README.md @@ -1,41 +1,11 @@ -# To-Do CLI Application +# todo-cli-cpp -This is a simple command-line to-do application written in C++. It allows users to manage their tasks efficiently through a straightforward interface. +C++ में एक छोटा कमांड-लाइन टु-डू ऐप जो `data/todos.txt` फ़ाइल में कार्यों को संग्रहीत करता है। -## Features +## जल्दी शुरू करें -- Add tasks to your to-do list -- Remove tasks from your to-do list -- List all tasks -- Load and save tasks to a file - -## Quick Start - -1. Clone the repository: - ``` - git clone https://github.com/yourusername/cpp-todo-cli.git - cd cpp-todo-cli - ``` - -2. Build the application using the Makefile: - ``` - make - ``` - -3. Run the application: - ``` - ./todo - ``` - -4. Use the following commands to manage your tasks: - - `add `: Add a new task - - `remove `: Remove a task by its ID - - `list`: List all tasks - -## Contributing - -We welcome contributions! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) file for guidelines on how to contribute to this project. - -## License - -This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. \ No newline at end of file +1. क्लोन करें: + ```bash + git clone https://github.com/yourname/todo-cli-cpp.git + cd todo-cli-cpp +git status \ No newline at end of file diff --git a/src/todo.cpp b/src/todo.cpp index b753b2f..0a01188 100644 --- a/src/todo.cpp +++ b/src/todo.cpp @@ -1,9 +1,8 @@ -// This file contains the implementation of the ToDo class and its associated methods for managing tasks. - #include "todo.h" #include #include #include +#include ToDo::ToDo() { loadTasks(); @@ -18,10 +17,12 @@ void ToDo::addTask(const std::string& task) { } void ToDo::removeTask(int index) { - if (index >= 0 && index < tasks.size()) { - tasks.erase(tasks.begin() + index); + // accept 1-based index (matches listTasks numbering) + int idx0 = index - 1; + if (idx0 >= 0 && static_cast(idx0) < tasks.size()) { + tasks.erase(tasks.begin() + idx0); } else { - std::cerr << "Invalid task index." << std::endl; + std::cerr << "Invalid task index: " << index << std::endl; } } @@ -32,7 +33,22 @@ void ToDo::listTasks() const { } void ToDo::loadTasks() { - std::ifstream file("data/todos.txt"); + tasks.clear(); + + std::filesystem::path dir("data"); + std::filesystem::path filePath = dir / "todos.txt"; + + if (!std::filesystem::exists(filePath)) { + // No file yet, nothing to load + return; + } + + std::ifstream file(filePath); + if (!file.is_open()) { + std::cerr << "Failed to open todo file for reading: " << filePath.string() << std::endl; + return; + } + std::string line; while (std::getline(file, line)) { tasks.push_back(line); @@ -40,7 +56,21 @@ void ToDo::loadTasks() { } void ToDo::saveTasks() const { - std::ofstream file("data/todos.txt"); + std::filesystem::path dir("data"); + std::filesystem::path filePath = dir / "todos.txt"; + + std::error_code ec; + if (!std::filesystem::exists(dir) && !std::filesystem::create_directories(dir, ec)) { + std::cerr << "Failed to create data directory: " << ec.message() << std::endl; + return; + } + + std::ofstream file(filePath, std::ios::out | std::ios::trunc); + if (!file.is_open()) { + std::cerr << "Failed to open todo file for writing: " << filePath.string() << std::endl; + return; + } + for (const auto& task : tasks) { file << task << std::endl; }