在C中省略参数名称

gan*_*892 1 c file-io

因此,我要遍历C语言中文件I / O的一些代码,并在临时Cat函数的一行上感到困惑。

我主要对main的这一行感到困惑:

void filecopy(FILE *, FILE *);
Run Code Online (Sandbox Code Playgroud)

我们没有指定要传递给ifp和ofp的文件的名称,所以我不确定这行在做什么。

/* filecopy: copy file ifp to ofp */
void filecopy(FILE *ifp, FILE *ofp) {

    int c; 

    while((c = getc(ifp)) != EOF){
        putc(c, ofp); 
    }
}


/* cat: concatenate files, version 1*/
int main(int argc, char **argv) {

    FILE *fp;
    void filecopy(FILE *, FILE *);

    if(argc == 1){ /*no args: copy standard input */
        filecopy(stdin, stdout);
    }else{
        while(--argc > 0){
            if((fp = fopen(*++argv, "r")) == NULL){
                printf("cat: can't open %s\n", *argv);
                return 1; 
            } else{
                filecopy(fp, stdout);
                fclose(fp); 
            }
        }
    }

    return 0; 
}
Run Code Online (Sandbox Code Playgroud)

dbu*_*ush 5

这行:

void filecopy(FILE *, FILE *);
Run Code Online (Sandbox Code Playgroud)

函数声明,用于允许其他代码调用函数。它指定存在具有给定名称和给定数量的已知类型参数的函数。不需要这些参数的名称即可调用该函数。仅在定义函数时(即,指定函数的主体时)才需要它们。

请注意,在此示例中,不需要声明,因为该函数在文件的前面已完全定义。