运行 从 Eclipse IDE 时如何为我的 C 程序指定命令行参数

How to specify command line arguments for my C program when running it from the Eclipse IDE

我有一些代码是从命令行执行的。它需要 3 个参数:

"example.txt" 3 s

我希望能够从我的 Eclipse IDE 内部 运行 这个程序,而不是从命令行 运行 安装它,但我不知道如何分配参数而不在程序中产生错误。

下面是主要方法:

int main(int argc, char **argv) {
if (argc != 4) {
    fprintf(stderr, "Usage: %s <input file> <num clusters> "
            "<linkage type>\n", argv[0]);
    exit(1);
} else {
    item_t *items = NULL;
    int num_items = process_input(&items, argv[1]);
    set_linkage(argv[3][0]);
    if (num_items) {
        cluster_t *cluster = agglomerate(num_items, items);
        free(items);

        if (cluster) {
            fprintf(stdout, "CLUSTER HIERARCHY\n"
                    "--------------------\n");
            print_cluster(cluster);

            int k = atoi(argv[2]);
            fprintf(stdout, "\n\n%d CLUSTERS\n"
                    "--------------------\n", k);
            get_k_clusters(cluster, k);
            free_cluster(cluster);
        }
    }
}
return 0;
}

我正在使用 C++ 和 Eclipse IDE。

您基本上需要为所述项目创建一个 debug/run 配置。转到 运行->Debug Configurations,select C/C++ Application,然后创建一个新配置。之后,您必须在“参数”选项卡上指定一些信息,例如应用程序、eclipse 项目和程序的参数

来自 here 的屏幕截图有点旧,但应该能给您思路。

之后,点击调试或 运行,Eclipse 应使用给定的参数启动您的程序。