作业2:实现cp命令
cp src dest
fread fwrite
先读,存起来char buf[256],然后写进去
循环直到读完为止
./a.out dest
#include <stdio.h>
#include<errno.h>
int find(const char*test)
{
FILE *fp=fopen(test,"rb");
if(fp==NULL)
{
perror("fp");
return 1;
}
fseek(fp,0,SEEK_END);
long size=ftell(fp);
fclose(fp);
return size;
}
int copy_file(const char *sc_file,const char *dest_file)
{
FILE *in_file=fopen(sc_file,"rb");
if(sc_file==NULL)
{
perror("sc_file");
return 1;
}
FILE *out_file=fopen(dest_file,"wb");
if(dest_file==NULL)
{
perror("out_file");
return 1;
}
int a=find(sc_file);
int buff[a];
a=fread(buff,1,a,in_file);
fwrite(buff,1,a,out_file);
fclose(in_file);
fclose(out_file);
}
int main(int argc, char* argv[])
{
if(argc!=3)
{
return 1;
}
copy_file(argv[1],argv[2]);
return 0;
}