簡體   English   中英

如果某個參數沒有出現在命令行中,我該如何使它無法運行?

[英]How do I make it so a program can't run if a certain argument doesn't appear in the command line?

我正在編寫一個程序,我在命令行輸入范圍 arguments 並運行一個循環,直到它到達范圍的末尾,然后將它們寫入文本文件

./cmdline -b 100 -e 200 -s 4 -f text.txt -m w  // -b = beginning; -e = end; -s = step size; -f = file path; -m = mode
100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 180 184 188 192 196 200

如果缺少 arguments、-b、-e、-s 范圍之一,我該如何使其無法運行。 除非在命令寫入文本文件后為模式寫入 r,否則它只會讀取文本文件。

您可以編寫以下代碼:

int main(int argc, char *argv[]) {
   if(argc != numbers_of_arguments) {
      printf("error message"); //a message you want
   }
   for(int i = 0; i < argc;) {
     if(strcmp(argv[i],"your range arguments") != 0) {
        printf("Error give these range arguments");
        return(-1);
     }
     i = i + 2; //cause of position of your range arguments
     if(i == 6) {
        break; // in order not proccess anything else because you reach your range arguments
     }
   }
   return(0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM