C compile error - "fgetc & fputc too few arguments"

C编译错误”获取和fgetc参数太少”

问题 (Question)

I'm new to C and struggling to see why this won't compile. The script should take two text files as args - read the first and copy the contents to the second. I'm getting the following error at compile:

root@debian:/home/kevin# gcc -Wall mycat.c -o mycat
mycat.c: In function ‘main’:
mycat.c:4:3: error: too few arguments to function ‘fgetc’
In file included from mycat.c:1:0:
/usr/include/stdio.h:533:12: note: declared here
mycat.c:5:5: error: too few arguments to function ‘fputc’

I'm not sure why it says fgetc should take more arguments because it's shown on my lecture slides taking one argument?

#include <stdio.h>
#include <stdlib.h>
int main(){
const char∗ ifilename = $1;
FILE∗ istream = fopen(ifilename, ”r”);
if (istream == NULL) {
fprintf(stderr, ”Cannot open %s \n”,ifilename);
exit(1);
}
const char∗ ofilename = ”$2”;
FILE∗ ostream = fopen(ofilename, ”w”);
if (ostream == NULL) {
fprintf(stderr, ”Cannot open %s \n” , ofilename);
exit(1);
}
int = c;
while ((c = fgetc(istream)) != EOF)
fputc(ostream);
return 0;
}

我是新来的C和难以明白为什么这不会编译。该脚本应该采取两个文本文件作为args -读的第一和第二内容复制。我收到以下错误在编译:

root@debian:/home/kevin# gcc -Wall mycat.c -o mycat
mycat.c: In function ‘main’:
mycat.c:4:3: error: too few arguments to function ‘fgetc’
In file included from mycat.c:1:0:
/usr/include/stdio.h:533:12: note: declared here
mycat.c:5:5: error: too few arguments to function ‘fputc’

我不知道为什么它说指针应该采取更多的参数,因为它对我的演讲幻灯片,以一个参数显示?

#include <stdio.h>
#include <stdlib.h>
int main(){
const char∗ ifilename = $1;
FILE∗ istream = fopen(ifilename, ”r”);
if (istream == NULL) {
fprintf(stderr, ”Cannot open %s \n”,ifilename);
exit(1);
}
const char∗ ofilename = ”$2”;
FILE∗ ostream = fopen(ofilename, ”w”);
if (ostream == NULL) {
fprintf(stderr, ”Cannot open %s \n” , ofilename);
exit(1);
}
int = c;
while ((c = fgetc(istream)) != EOF)
fputc(ostream);
return 0;
}

最佳答案 (Best Answer)

I was bored so I coded from scratch. Tested it right now and it worked. Maybe you can get your mistakes from my code (After all the algorithm I used is the same as yours)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]){
    FILE* filesrc = NULL;
    FILE* filedest = NULL;
    int c = 0;

    if(argc != 3){
        fprintf(stderr,"Usage: programname sourcefile destinationfile");
        return EXIT_FAILURE;
    }

    if((filesrc = fopen(argv[1],"r")) == NULL){
        fprintf(stderr,"Cannot open sourcefile!");
        return EXIT_FAILURE;
    }
    if((filedest = fopen(argv[2],"w")) == NULL){
        fprintf(stderr,"Cannot open destinationfile!");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(filesrc)) != EOF)
        fputc(c,filedest);

    return EXIT_SUCCESS;
}

To call the program type in console: ./programname sourcefilename destinationfilename Your major mistake is your argument handling, or it's a completely new c feature I never heard of ;) Arguments are passed to the main() function as an array of strings (char pointers) argv[0] = program name || argv[1] = 1st argument || argv[2] = 2nd argument etc... And as already mentioned: you forgot the first argument for the fgetc() (your variable 'c' that is)

我很无聊,所以我从头开始编码实现。测试了它现在的工作。也许你可以从我的代码得到你的错误(所有的算法,我使用后和你的是一样的)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]){
    FILE* filesrc = NULL;
    FILE* filedest = NULL;
    int c = 0;

    if(argc != 3){
        fprintf(stderr,"Usage: programname sourcefile destinationfile");
        return EXIT_FAILURE;
    }

    if((filesrc = fopen(argv[1],"r")) == NULL){
        fprintf(stderr,"Cannot open sourcefile!");
        return EXIT_FAILURE;
    }
    if((filedest = fopen(argv[2],"w")) == NULL){
        fprintf(stderr,"Cannot open destinationfile!");
        return EXIT_FAILURE;
    }

    while ((c = fgetc(filesrc)) != EOF)
        fputc(c,filedest);

    return EXIT_SUCCESS;
}

电话控制台程序类型:。/ programname sourceFileName destinationfilename 你的主要错误是你的论点的处理,或这是一个完全新的C功能我从来没有听说过;)参数是一个字符串数组传递给main()功能(char指针)参数[ 0 ] =程序名称| | argv [ 1 ] =第一参数| | argv [ 2 ] =第二参数等。正如已经提到:你忘了fgetc()第一个参数(变量c是)

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22077321