c - 如何在Ubuntu中用C语言使用命令行参数运行这个程序?

标签 c ubuntu command-line-arguments

我希望能够使用 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/

相关文章:

php - 文件 SRC 路径在部署时自动更改

python - 指定要使用 argparse 运行的命令

c - 千兆字节 (GB) 是多少字节?

c - 为什么行不通过调用 gtk_list_store_set 函数在 TreeView 中更新

bash - 使用 Matlab 中的 shell 命令

c# - 从 Mono C# 运行 Bash 命令

batch-file - 读取批处理文件中参数的每个字符

java - Gradle:从命令行将参数传递给编译器

c++ - 在 Visual C++ 中全局覆盖 malloc

c - 如何理解和修复为嵌入式系统编译 Linux 内核时出现的错误