[英]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.