检索命令行参数时出错

时间:2016-04-13 17:42:28

标签: c++

要运行我的程序,您必须输入:

(program name) -s (a number) -f (the file name) 

我遇到的问题是文件名在string,命令参数在char

这是我的代码,以便我可以更清楚地说明我的问题:

if(argc==1){
   cout << "Please Enter it in the Form of (prog -s (number of state) -f (file name)\n";
}   
if(argc==2){
   cout << "Please Enter it in the Form of (prog -s (number of state) -f (file name)\n";
}   
if(argc==3){
   cout << "Please Enter it in the Form of (prog -s (number of state) -f (file name)\n";
}   
if(argc==4){
   cout << "Please Enter it in the Form of (prog -s (number of state) -f (file name)\n";
}   
if(argc==5){

      if((argv[1][0]=='-'&& argv[1][1]=='s')&&(argv[3][0]=='-'&& argv[3][1]=='f')){
        int x;
         x=argv[2][0]-48;
         state *array;
         array= create_states(x);

         ifstream File;
         File.open(argv[4][0]);

在这一行:

file.open(argc[4][0]);

我收到错误。

1 个答案:

答案 0 :(得分:0)

应该是:

  ifstream File;
  File.open(argv[4]);

此外,如果文件路径包含空格,请使用双引号。