Hello. I need to determine the arguments of the command line and based on the received commands to do certain actions. Running the program out of the parameters with different parameters I get the message Segmentation fault . Help me understand what I'm doing wrong. I apologize in advance for such a terrible code.

 int main(int argc, char* argv[]) { MySniffer snf; if(argc == 1 || argc == 3 || argc > 4) { cout << "Wrong options. Read program options.\n" << snf.usage_inf() << endl; exit(1); } for(int i = 1; i < argc; ++i) { if(strcmp(argv[i], "select") == 0) { cout << "hello" << endl; for(int j = i; j < argc; ++j){ if(strcmp(argv[j], "iface") == 0){ string str = string(argv[3]); snf.setIface(str); snf.pkgMonitor(snf.get_cmd()); } } } if(strcmp(argv[i], "show") == 0) { for(int j = i; j < argc; ++j) { if(strcmp(argv[j], " count")){ if(snf.select_data(argv[2])==1){ cout << "There is no adress " << argv[2] << " or data base wasn't created.\n" << "Check if you select network interface.\n"; } snf.pkgMonitor(snf.get_cmd()); } } } if(strcmp(argv[i], "--help") == 0) { for(int i = 0; i < argc; ++i) cout << argv[i] << endl; cout << snf.usage_inf() << endl; snf.pkgMonitor(snf.get_cmd()); } if(strcmp(argv[i], "stop") == 0) { cout << "Packets are not sniffed.\n" << endl; snf.set_cmd(true); } if(strcmp(argv[i], "start") == 0){ if(snf.isIfaceDeff()){ cout << "Packets are being sniffed from now on\n" << endl; } else { cout << "You first need to select your interface. Read program options.\n" << snf.usage_inf() << endl; } snf.set_cmd(false); snf.pkgMonitor(snf.get_cmd()); } } return 0; } 

Closed due to the fact that off-topic participants Vlad from Moscow , αλεχολυτ , cheops , Denis , aleksandr barakin 24 Oct '16 at 7:45 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Vlad from Moscow, αλεχολυτ, cheops, Denis, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Despite the clumsy code, I think that the problem is related to one of the functions called from main. - Vlad from Moscow
  • @Vlad from Moscow, found an error in one of these functions. I now delete the answer or how best to do? - Vova Polischuck
  • I think it's best to just delete the answer. - Vlad from Moscow
  • I meant "delete the question." :) - Vlad from Moscow
  • @Vlad from Moscow, I, too, but after clicking "delete" I received a message that they would forbid me to ask questions) scary) - Vova Polischuck

1 answer 1

You can only have argc==2 and argc==4 , judging by the line

 if(argc == 1 || argc == 3 || argc > 4) { 

One of the questions is what will happen here.

 if(strcmp(argv[j], "iface") == 0){ string str = string(argv[3]); 

with argc == 2 ? ...

The same with

 if(snf.select_data(argv[2])==1){ cout << "There is no adress " << argv[2] << " or data base wasn't created.\n" 

And by the way, in

 if(strcmp(argv[i], "select") == 0) { cout << "hello" << endl; for(int j = i; j < argc; ++j){ if(strcmp(argv[j], "iface") == 0){ 

what's the point of comparing with iface the argument that is obviously equal to select - I mean, to start with int j = i ?

  • Suppose that with argc == 2 , either stop or start should be called. Until if(strcmp(argv[j], "iface") == 0){ string str = string(argv[3]); should not reach. - Vova Polischuck
  • Then you have to look with specific data in the hands :) “Suppose” is a bad method of searching for errors ... - Harry
  • I agree. but for me at this stage it would work too much ) - Vova Polischuck