I'm attempting to recreate the Unix shell program "todo.txt" using C++ on Windows. An example use of my program in the Command Prompt would be:
>todo /A "I like cheese."
which would append the item "I like cheese." to the file "todo.txt". In my program, there is a line which checks the arguments for the "/A" trigger:
if (argv[1] == "/A") {
... }
My problem is that when I use the "/A" trigger in the Command Prompt, my program returns the previous if statement as false (???). I've noticed that the argument "argv[]" is an array of char arrays:
int main (int argc, char* argv[]) {
... }
so I have attempted to check for the "/A" argument against another array of chars instead of a string:
char A[2] = {'/', 'A'};
if (argv[1] == A) {
... }
but this also returns false. I've attempted to debug this by printing out what argv[1] is in the console, and it is in fact the string "/A" but for some reason
(argv[1] == "/A")
insists on returning false. The funny this is it was working when the program was simpler and then decided to make no sense later on. Anyone have any ideas? Thanks.
My program is still pretty small, so I'll post it here:
/* This project is an attempt to recreate Gina Trapani's
* todo.txt Unix shell CLI in C++ for Windows. I understand
* that one can most likely use her program on Windows in
* some fashion, but this is mostly a learning experience. */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main (int argc, char* argv[]) {
// Check to make sure the user has input the correct number of arguments
if ((argc < 3) | argc > 3) {
cout << "The syntax of the command is incorrect.\n";
} else {
// Debugging
cout << "argc == " << argc
<< "\nargv[0] == " << argv[0]
<< "\nargv[1] == " << argv[1]
<< "\n\n";
// Check if the user is adding a todo item
// Alternate check (char array instead of string):
/*char A[2] = {'/', 'A'};
if (argv[1] == A) {*/
if (argv[1] == "/A") {
// Print the item to the todo.txt text file
ofstream todo ("todo.txt", ios::app);
if (todo.is_open()) {
todo << argv[2] << "\n";
} else {
cout << "Unable to open todo.txt. Please check your permissions.\n";
}
// Check if the user is requesting help
} else if (argv[1] == "/?") {
cout << "A shell interface to manage a todo text file.\n\n"
<< "TODO (/A | /R) item\n\n"
<< " item Specifies the item to be modified in the todo text file.\n"
<< " /A Indicates the item will be added to the todo text file.\n"
<< " /R Indicates the item will be removed from the todo text file.\n";
} else {
cout << "The syntax of the command is incorrect.\n";
}
}
return 0;
}
*Edit: I followed the instructions from the following tutorial from cplusplus.com: http://j.mp/g4gipR