gpt4 book ai didi

C 编译错误 - "fgetc & fputc too few arguments"

转载 作者:行者123 更新时间:2023-11-30 20:06:41 25 4
gpt4 key购买 nike

我是 C 语言新手,正在努力了解为什么它无法编译。该脚本应采用两个文本文件作为参数 - 读取第一个并将内容复制到第二个。我在编译时遇到以下错误:

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’

我不确定为什么它说 fgetc 应该接受更多参数,因为它在我的讲座幻灯片上显示为接受一个参数?

#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;
}

最佳答案

我很无聊,所以我从头开始编码。现在测试了一下,效果很好。也许你可以从我的代码中得到你的错误(毕竟我使用的算法和你的一样)

#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;
}

要在控制台中调用程序类型:./程序名源文件名目标文件名你的主要错误是你的参数处理,或者它是一个我从未听说过的全新的 c 功能;) 参数作为字符串数组(字符指针)传递给 main() 函数 argv[0] = 程序名称 || argv[1] = 第一个参数 || argv[2] = 第二个参数等...正如已经提到的:您忘记了 fgetc() 的第一个参数(即您的变量“c”)

关于C 编译错误 - "fgetc & fputc too few arguments",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22077321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com