下面程序的功能是将一个磁盘中的二进制文件复制到另一个磁盘中,两个文件名随命令行一起输入,输入时原有文件的文件名在前,新复制文件的文件名在后。请填空补全。(注意:填写时不要加额外空格) #include <stdio.h> int main(int argc,char *argv[]) { FILE *old,*new; char ch; if(argc!=3) { printf(“You forgot to enter a filename\n”); exit(0); } if((old=fopen( 2 分 ," 2 分 "))==NULL) { printf(“cannot open infile\n”); exit(0); } if((new=fopen( 2 分 ," 2 分 "))==NULL) { printf(“cannot open outfile\n”); exit(0); } while(!feof(old)) fputc( 2 分 ,new); fclose(old); fclose(new); return 0; }
时间: 2024-02-10 21:06:13 浏览: 32
以下是完整代码:
```c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *old, *new;
char ch;
if (argc != 3) {
printf("You forgot to enter a filename\n");
exit(0);
}
if ((old = fopen(argv[1], "rb")) == NULL) {
printf("cannot open infile\n");
exit(0);
}
if ((new = fopen(argv[2], "wb")) == NULL) {
printf("cannot open outfile\n");
exit(0);
}
while (!feof(old)) {
ch = fgetc(old);
fputc(ch, new);
}
fclose(old);
fclose(new);
return 0;
}
```
解释一下:首先通过命令行输入两个文件名,分别为原有文件的文件名和新复制文件的文件名。然后在 main() 函数中,利用 fopen() 函数分别打开这两个文件,如果打开失败,则输出错误信息并退出程序。接着使用 fgetc() 和 fputc() 函数来逐个读取原有文件中的字符,并将其写入新复制文件中,直到文件末尾。最后关闭两个文件并返回 0。