Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
来源:互联网 发布:淘宝确认收货前退货 编辑:程序博客网 时间:2024/05/16 23:47
- 通过系统调用实现 file.copy
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>int main(int argc, char *argv[]){ if (argc != 3) { printf("usage : ./copy filename1 filename2\n"); } char buffer[1024] = {0}; int fd1 = open(argv[1], O_RDWR);/*读写方式打开文件1*/ if (-1 == fd1) /*出错判断*/ { perror("open1"); return 1; } int fd2 = open(argv[2], O_RDWR | O_CREAT, S_IRWXU);/*创建新文件并以读写方式打开*/ if (-1 == fd2) { perror("open2"); return 2; } int count = 0; while (count = read(fd1, buffer, 1024)) { if (-1 == count) { perror("read1"); close(fd1); close(fd2); return 3; } int coun2 = write(fd2, buffer, count); if (-1 == count) { perror("write1"); close(fd1); close(fd2); return 4; } memset(buffer, 0, 1024); /*清空数组*/ } close(fd1); close(fd2); return 0;}
- 通过库函数实现 file.copy
#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){ if (argc != 3) { printf("usage : ./copy filename1 filename2\n"); } char buffer[1024] = {0}; FILE *file1 = fopen(argv[1], "r+"); if (NULL == file1) { perror("fopen"); return 1; } FILE *file2 = fopen(argv[2], "w+"); if (NULL == file2) { perror("fopen"); return 2; } int count = 0; count = fread(buffer, sizeof(char), 1024, file1); if(0 == count) /*出错判断*/ { perror("fread"); fclose(file1); fclose(file2); return 3; } fseek(file1, 0, SEEK_SET); /*从头再进行读写*/ while (count = fread(buffer, sizeof(char), 1024, file1)) { int count2 = fwrite(buffer, sizeof(char), count, file2); if (count2 == 0) { perror("fwrite"); fclose(file2); fclose(file1); return 4; } memset(buffer, 0, 1024); } fclose(file1); fclose(file2); return 0;}
阅读全文
0 0
- Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
- Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
- Linux下通过系统调用(API)和C语言库函数对文件进行访问
- Linux下通过系统调用(API)和C语言库函数对文件进行访问
- Linux下通过系统调用(API)和C语言库函数对文件进行访问
- Linux下通过系统调用(API)和C语言库函数对文件进行访问
- Linux下通过系统调用(API)和C语言库函数对文件进行访问
- linux系统下库函数和系统调用都对文件的基本操作
- linux下用函数和系统调用分别实现计算文件长度(不解) 学习笔记
- linux下的系统调用和库函数调用
- 文件拷贝函数(使用系统调用和C库函数)
- linux下调用库函数实现文件的读写
- linux 文件锁、系统调用和库函数之间的关系
- Linux下的C语言编程——系统调用read和write函数实现文件拷贝
- 1、win10下连接本地系统上的Linux操作系统(分别以Nat方式和桥接模式实现)
- linux系统调用和库函数的区别
- 操作系统 之 库函数和系统调用
- 对man文件的综述--系统调用和库函数的关系
- Java课设--购物清单
- Qt C++中的关键字explicit
- #1 Ember中的对象
- 关于hive 中order by,sort by,ditribute by,cluster by的解释说明
- C++易错题
- Linux操作系统下,通过系统调用和库函数分别实现对文件的拷贝
- 转载 elm中文手册
- 条件if-else
- ubuntu软件中心打不开解决办法
- linux命令操作
- HDU 5893 List wants to travel (树链剖分,线段树区间合并)
- The Flash Memory Read-out protection option must be disabled for debug.
- 自然图像抠图/视频抠像技术发展情况梳理(image matting, alpha matting, video matting)--计算机视觉专题1
- 神经网络系统画画——在windows下实现