I have a problem with file handling. More precisely I've written a code for exercising but after a time it won't make the .txt file. I don't know if the error is my computer or my code but I hope someone knows the answer. It's not the final stage but the problem started here. (This is a simple console application)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int Counter()
{
static int taskID;
cout << taskID++ << " - ";
}
int main(int argc, char const *argv[])
{
char inputTask[256];
string outputTask;
string listAllTask = "-l";
string addNewTask = "-a";
string removeTask = "-r";
string completeTask = "-c";
if (argc == 1) {
cout << "CLI Todo application" << endl;
cout << "====================\n" << endl;
cout << "Command line arguments:" << endl;
cout << "-l Lists all the tasks" << endl;
cout << "-a Adds a new task" << endl;
cout << "-r Removes a task" << endl;
cout << "-c Completes an task" << endl;
} else if (argc == 2) {
//create an object to access the file that containing the tasks
fstream todoApp("tasks.txt", ios::ate | ios::in | ios::out);
if (todoApp.is_open()) {
if (string(argv[1]) == listAllTask){
while (!todoApp.eof()) {
todoApp.seekg(0, ios::end);
if (todoApp.tellg() == 0) {
cout << "No todos for today! :)" << endl;
} else {
getline(todoApp, outputTask);
cout << Counter << outputTask << endl;
}
}
} else if (string(argv[1]) == addNewTask) {
cout << "Enter the new todo:" << endl;
todoApp.seekg(0, ios::end);
cin.getline(inputTask, 120);
todoApp << inputTask << endl;
todoApp.close();
cout << "-- your task has been saved --" << endl;
}
} else {
cerr << "Something went wrong, please, try again." << endl;
}
//todoApp.close();
} else {
cout << "Too many arguments!!!" << endl;
}
return 0;
}