我希望能够使用 int main(argc, **argv[])
,以便当我在终端上键入 clr
时,它将清除终端屏幕。我只是不知道如何使用命令行参数。
#include <stdio.h>
#include <stdlib.h>
int main(){
int count;
char buffer[100];
char i = 32;
int p=0;
char* clr = "clr";
char* quit = "quit";
scanf("%s", buffer);
for(count = 0; count < i; count++){
buffer[count];
}
printf("The first word of your line is: %s\n",buffer);
if( buffer[p] == *clr){
system("cls");
} else if(buffer[p] == *quit){
exit(0);
}
return 0;
}
最佳答案
去掉程序中所有无用的东西,然后使用strcmp()
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char buffer[100];
char *clr = "clr";
char *quit = "quit";
scanf("%s", buffer);
printf("The first word of your line is: %s\n", buffer);
if (strcmp(buffer, clr) == 0) {
system("cls");
} else if (strcmp(buffer, quit) == 0) {
exit(0);
}
return 0;
}
根据您的问题,我认为您没有在寻找与命令行参数有关的任何内容。
如果你真的想“在终端上输入 clr
”并让它清除屏幕(使用这个可怕的 system()
方法),那么只需执行以下操作:
#include <stdlib.h>
int main(void) {
system("cls");
return 0;
}
将可执行文件命名为clr
,并将其放入路径中的文件夹中。
关于c - 如何在Ubuntu中用C语言使用命令行参数运行这个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072735/