gpt4 book ai didi

c - 文件处理程序中不需要的输出(连接文件)

转载 作者:行者123 更新时间:2023-11-30 21:03:28 27 4
gpt4 key购买 nike

我在 K&R C 编程书中读到了这段代码。我用这些奇怪的盒子来代替文字。为什么这样?另外请解释一下这个程序的if语句为什么他在fileopen期间传递argv作为参数。我用“path”而不是 argv 打开文件,但是这个命令行 vector 参数让我更难理解。这里的 *argv[] 是什么?我认为到目前为止我还没有向 argv[index] 提供任何字符串。这对我来说有点困惑。提前致谢。

What are these weird symbols i wrote words here

#include <stdio.h>
/* cat: concatenate files, version 1 */
main(int argc, char *argv[])
{
FILE *fp;
void filecopy(FILE *, FILE*);
if(argc==1) /*No optional argument so printing stdin and stdout*/
filecopy(stdin, stdout);
while(--argc0)
if((fp= fopen(*++argv, "r")) == NULL){
printf("Cat: can't open %s\n", *argv);
return 1;
}else{
filecopy(fp, stdout);
fclose(fp);
}
return 0;
}

/*filecopy program to copy file a to b*/
void filecopy(FILE *a, FILE *b){
while(c=getc(a) != EOF)
putc(c,b);
}

最佳答案

看来您从 K&R 复制程序时犯了一些错误。..他们正在给你带来问题。

具体来说,这段代码:

while(c=getc(a) != EOF)
putc(c,b);

应该是:

int c;    

while((c=getc(a)) != EOF)
putc(c,b);

问题在于赋值运算符 (=) 的求值顺序较低比不等于运算符 (!=) 。

关于c - 文件处理程序中不需要的输出(连接文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24747011/

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