Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何对多个文件使用C程序_C_Bash - Fatal编程技术网

如何对多个文件使用C程序

如何对多个文件使用C程序,c,bash,C,Bash,我有一个C程序,它有两个参数,第一个是filename.txt,第二个是一些参数。最后,生成一些分数。现在,我在名为1.txt的文件夹中有10000个文件。。。。10000.txt,我想用这个C程序计算所有10000个文件的分数,并把所有分数放在result.txt文件中 如何使这件事自动化。你可以做两件事 编写一个外部脚本,以所有文件作为参数顺序调用程序 更改程序,使其在所有文件上循环。您可以使用sprintf对文件名进行迭代 char filename[50]; for (int i =

我有一个C程序,它有两个参数,第一个是filename.txt,第二个是一些参数。最后,生成一些分数。现在,我在名为1.txt的文件夹中有10000个文件。。。。10000.txt,我想用这个C程序计算所有10000个文件的分数,并把所有分数放在result.txt文件中

如何使这件事自动化。

你可以做两件事

  • 编写一个外部脚本,以所有文件作为参数顺序调用程序
  • 更改程序,使其在所有文件上循环。您可以使用sprintf对文件名进行迭代

     char filename[50];
     for (int i = 1; i < 10000; ++i) {
       sprintf(filename, "%d.txt", i);
       FILE* file = fopen(filename);
       // .. do stuff
       fclose(file);
     }
    
    char文件名[50];
    对于(int i=1;i<10000;++i){
    sprintf(文件名为“%d.txt”,i);
    FILE*FILE=fopen(文件名);
    //…做事
    fclose(文件);
    }
    
  • 树路

    第一个是通过在参数中给出所有文件:

    ./myProgram 1.txt-args 2.txt-args

    第二种方法是给你所有的文件名,然后给你参数 ./myProgram*.txt-args

    第三种方法是使用循环编写程序,在循环中查找所有文件

    C程序:

    #include <stdio.h>
    
    int main (int argc, char *argv[])
    {
        /* open file argv[1] with options in argv[2] */
        /* ... */
        return 0;
    }
    

    你可以这样做:

  • 作为参数传递存储文件的目录名。给 当您有这么多的文件时,所有文件名作为参数是不可能的 那些我不确定C标准的当前版本是什么,但是 ANSIC至少提到了编译器应该能够 至少需要处理31个函数参数,因此拥有10000个参数是非常重要的 数额很大

  • 然后使用例如
    readdir()
    函数迭代给定目录中的所有文件,并执行您需要执行的工作


  • 请展示一些代码,你尝试过什么?我希望你听说过循环?你需要为C程序获取参数的代码,还是需要一些shell命令来为你的C程序提供文件列表?第一个不起作用,他希望合并所有文件的数据,而不是单独对它们进行操作。这取决于OP如何操作。我会让脚本聚合数据,但这取决于需求。可能是原程序无法修改的情况。
    ./program directory/*.txt