2017-04-23 9 views
-1
 int main(int argc, char *argv[]) { 
    int opt= 0; 
    int start = -1, end = -1; 
    char *alg,*dir,*graph; 
    //Specifying the expected options 
    //The two options s and e expect numbers as argument 
    static struct option long_options[] = { 
    {"start",no_argument,0,'s' }, 
    {"end",no_argument,0,'e' }, 
    {"algorithm",no_argument, 0,'a' }, 
    {"directory",required_argument, 0,'d' }, 
    {"graph",required_argument,0,'g' }, 
    {0,0,0,0} 
    }; 

    int long_index =0; 
    int i=0,j=0; 
    size_t size = 1; 
    while ((opt = getopt_long(argc, argv,"s:e:a:d:h:g:", 
      long_options, &long_index)) != -1) { 
     switch (opt) { 
     case 'd' : 
       dir = optarg; 

        if (optarg == NULL) 
        printf("d option is must"); 
        else if 
        { 
        // How to verify the path provided is Correct/existing 
        } 
        else 
         { 
         // Any more options of error handling on path 
         provided is available ? 
         } 

ユーザーは、オプション-dを使用してディレクトリパスを渡します。パスが正しいかどうかを確認するにはどうすればよいですか?ユーザによって入力されたディレクトリパスに対してCでエラー処理が可能ですか?

これには他にどのようなエラーチェックが含まれていますか?

+0

まず、ロジックを追うのは難しいというコード自体を改善してください。 –

+1

['stat'](http://man7.org/linux/man-pages/man2/stat.2。 html)または['access'](http://man7.org/linux/man-pages/man2/access.2.html)または同様の機能ですか? –

答えて

0

あなたはそれを開いて、私はこれがあなたが求めているものであるかわからないけど、それは

FILE *infile=fopen("<directory>/<filename>","r"); 
if(infile==NULL){ 
    printf("File did not open\n"); //implies directory or filename is invalid 
} 

を開いたかどうかを確認、そのディレクトリ内のファイルを開こうとしている場合。 moreを指定してください

関連する問題