프로그램을 작성 중이며 명령 인수를 허용하려고합니다. 그러나 인수를 전달할 때 start가 항상 호출되는 것처럼 작동합니다. 내 코드는 아래와 같습니다. 어떤 도움을 주셔서 감사합니다.C 프로그램이 인수를 올바르게 읽지 못함.
//compile to iservices
// Ilkotech Services - C Core
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s start|shutdown|reload\n",argv[0]);
}
else {
if (strcmp(argv[1],"start") != 0) {
services_start();
}
else if (strcmp(argv[1],"shutdown") != 0) {
services_shutdown();
}
else if (strcmp(argv[1],"reload") != 0) {
services_reload();
}
else {
printf("%s is not a valid argument\n",argv[1]);
printf("Usage: %s start|shutdown|reload\n",argv[0]);
exit(1);
}
}
return 0;
}
int services_start() {
printf("Started.\n");
return 0;
}
int services_shutdown() {
printf("Shutting down!\n");
return 0;
}
int services_reload() {
printf("Reloading services configuration.\n");
return 0;
}
이것은 효과가 있습니다. 감사! – JohnHoulderUK