简体   繁体   English

如果某个参数没有出现在命令行中,我该如何使它无法运行?

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

I'm writing a program where I input range arguments at the command line and run a loop until it gets to the end of the range and then write them to a text file我正在编写一个程序,我在命令行输入范围 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

How do I make it so the program can't run if one of the range arguments, -b, -e, -s, are missing.如果缺少 arguments、-b、-e、-s 范围之一,我该如何使其无法运行。 Unless an r is written for the mode after the command is written to a text file, then it just reads the text file.除非在命令写入文本文件后为模式写入 r,否则它只会读取文本文件。

You can write the code below:您可以编写以下代码:

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.

相关问题 书中的命令行参数程序不起作用…(C) - Command-line argument program from book doesn't work…(C) 如何在此程序中添加一个 do-while 循环,以便如果输入小于 4,它不会打印任何内容 - How do I add a do-while loop in this program so that if the input is less than 4 it doesn't print anything 在Eclipse for C程序中进行调试找不到命令行参数文件 - Debugging in Eclipse for C program can't find command line argument file 命令行参数来运行ac程序 - command line argument to run a c program 如何使用读取功能读取文件时,数据不会被切断 - How can I make it so my data doesn't get cut off when reading a file using the read function 无法重命名包含命令行参数的字符串 - Can't rename the string that contains a command line argument 我似乎无法运行程序和 printf - I can't seem to run the program and printf 更改c中的程序,因此它需要一个可选的命令行参数* infile * - changing a program in c, so it takes an optional command line argument *infile* 如何使函数通过命令行打印出一定数量的随机数? C - How do I make a function print out a certain amount of Random numbers through the command line? C 如何编译 c 程序使其不依赖任何库? - How to compile c program so that it doesn't depend on any library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM