这个需要参数的C程序是什么意思

来源:百度知道 编辑:UC知道 时间:2024/05/25 00:37:26
需要什么参数才会正确运行?

#include <stdio.h>
#include <errno.h>
#define BUF_SIZE 256
int main (int argc, char *argv[])
{
FILE *in_file, *out_file;
char rec[BUF_SIZE];
size_t bytes_in, bytes_out;
if(argc!=3){
printf("Usage: cpC file1 file2\n");
return 1;
}
in_file=fopen(argv[1], "rb");
if(in_file==NULL) {
perror(argv[1]);
return 2;
}
out_file = fopen (argv[2], "wb");
if (out_file== NULL) {
perror (argv[2]);
return 3;
}
while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file))>0 ){
bytes_out = fwrite (rec, 1, bytes_in, out_file);
if (bytes_out !=bytes_in) {
perror ("Fatal write error.");
return 4;
}
}
fclose (in_file);
fclose (out_file);
return 0;
}

假设文件名是app~
在命令app 参数1 参数2 就可以了~
参数多了会printf("Usage: cpC file1 file2\n");

参数1是要打开读取的文件路径

参数2是要写入的文件路径

功能是打开参数1的文件 读出后写入参数2文件

功能就像命名的名字cpC copy Content.
内容拷贝,将第一个文件的内容拷贝到第二个文件中。

注:会覆盖第二个文件原先的内容。