I have the following code:
int main(int argc, char *argv[])
{
if(strcmp(argv[1],"-e")==0)
{
//perform code
}
//code if argv[1] is not "-e"
return 0;
}
When I take out the whole if statement:
if(strcmp(argv[1], "-e")==0)
my code if strcmp(argv[1], “-e”) == 0) is not “-e” works fine. But when I leave it in, my code only works for when the if statement above produces true. Any ideas on why this may happen? For example:
If I compile with argv[1] not commented out:
//example program will be an executable
exampleProgram -e < a.txt works but exampleProgram < a.txt doesn’t work. Any ideas why?
Simple, if you don’t send any argument to the program then
args[1]doesn’t exist and you are trying to access to an unassigned memory position.You may do something like this: