Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 8 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 <task>`: Add a new task
- `remove <task_id>`: 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.
1. क्लोन करें:
```bash
git clone https://github.com/yourname/todo-cli-cpp.git
cd todo-cli-cpp
git status
44 changes: 37 additions & 7 deletions src/todo.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// This file contains the implementation of the ToDo class and its associated methods for managing tasks.

#include "todo.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <filesystem>

ToDo::ToDo() {
loadTasks();
Expand All @@ -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<size_t>(idx0) < tasks.size()) {
tasks.erase(tasks.begin() + idx0);
} else {
std::cerr << "Invalid task index." << std::endl;
std::cerr << "Invalid task index: " << index << std::endl;
}
}

Expand All @@ -32,15 +33,44 @@ 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);
}
}

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;
}
Expand Down