linux c两种复制文件的方法

第一种方法:例如 linux 下的系统调用


    
    
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. char buff[ 1024];
  9. int len;
  10. int main(int argc, char const *argv[])
  11. {
  12. char const *src_path = argv[ 1];
  13. char const *des_path = argv[ 2];
  14. int fd,fd2;
  15. fd = open(src_path,O_RDWR|O_CREAT);
  16. fd2 = open(des_path,O_RDWR|O_CREAT);
  17. while(len = read(fd,buff, 1024))
  18. {
  19. write(fd2,buff,len);
  20. }
  21. return 0;
  22. }

这里主要 用到的函数是 open,read,write


其中open 函数:

第一个参数为要打开文件的路径,第二个参数是功能flag,O_RDWR表示以读写方式打开文件,O_CREAT表示如果文件不存在则创建

返回一个文件描述符,表示打开了的文件


其中 read 函数:

第一个参数是一个文件描述符,表示:从该文件描述符表示的文件读取数据

第二个参数是把从文件读到的信息放在一个缓冲数组中

第三个参数是读一次读多少字节


其中 write 函数:

第一个参数,是把数据写到哪里(写到哪个文件描述符中)

第二个参数:把缓冲数组中的数据写到文件描述符中

第三个参数:表示一次写多少字节


注意:最好向上面代码那样用一个while循环来读写数据,这样的话,read 中的第三个参数就不用设置成太大,因为他会把数据全读完才退出循环


第二种方法:用文件流


    
    
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. char buff[ 1024];
  6. int len;
  7. int main(int argc, char const *argv[])
  8. {
  9. FILE *in,*out;
  10. char const * src_path = argv[ 1]; //要被拷贝的文件路径
  11. char const * des_path = argv[ 2]; //拷贝的文件放在哪里(路径)
  12. in = fopen(argv[ 1], "r+");
  13. out = fopen(argv[ 2], "w+");
  14. while(len = fread(buff, 1, sizeof(buff),in))
  15. {
  16. fwrite(buff, 1,len,out);
  17. }
  18. return 0;
  19. }

两种方法其实有异曲同工之妙,第一种方法的文件描述符 = 第二种方法的文件流指针 in 和 out。


第一种方法:例如 linux 下的系统调用


  
  
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. char buff[ 1024];
  9. int len;
  10. int main(int argc, char const *argv[])
  11. {
  12. char const *src_path = argv[ 1];
  13. char const *des_path = argv[ 2];
  14. int fd,fd2;
  15. fd = open(src_path,O_RDWR|O_CREAT);
  16. fd2 = open(des_path,O_RDWR|O_CREAT);
  17. while(len = read(fd,buff, 1024))
  18. {
  19. write(fd2,buff,len);
  20. }
  21. return 0;
  22. }

这里主要 用到的函数是 open,read,write


其中open 函数:

第一个参数为要打开文件的路径,第二个参数是功能flag,O_RDWR表示以读写方式打开文件,O_CREAT表示如果文件不存在则创建

返回一个文件描述符,表示打开了的文件


其中 read 函数:

第一个参数是一个文件描述符,表示:从该文件描述符表示的文件读取数据

第二个参数是把从文件读到的信息放在一个缓冲数组中

第三个参数是读一次读多少字节


其中 write 函数:

第一个参数,是把数据写到哪里(写到哪个文件描述符中)

第二个参数:把缓冲数组中的数据写到文件描述符中

第三个参数:表示一次写多少字节


注意:最好向上面代码那样用一个while循环来读写数据,这样的话,read 中的第三个参数就不用设置成太大,因为他会把数据全读完才退出循环


第二种方法:用文件流


  
  
  1. #include <stdio.h>
  2. #include <dirent.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. char buff[ 1024];
  6. int len;
  7. int main(int argc, char const *argv[])
  8. {
  9. FILE *in,*out;
  10. char const * src_path = argv[ 1]; //要被拷贝的文件路径
  11. char const * des_path = argv[ 2]; //拷贝的文件放在哪里(路径)
  12. in = fopen(argv[ 1], "r+");
  13. out = fopen(argv[ 2], "w+");
  14. while(len = fread(buff, 1, sizeof(buff),in))
  15. {
  16. fwrite(buff, 1,len,out);
  17. }
  18. return 0;
  19. }

两种方法其实有异曲同工之妙,第一种方法的文件描述符 = 第二种方法的文件流指针 in 和 out。


猜你喜欢

转载自blog.csdn.net/leo062701/article/details/81097480
今日推荐