linux 文件操作 --copy_file
2015-08-11 12:34
681 查看
代码:
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat copy_file.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 1024
#define SRC_FILE_NAME "src_file"
#define DEST_FILE_NAME "dest_file"
#define OFFSET 10240
int main()
{
int src_file, dest_file;
unsigned char buff[BUFFER_SIZE];
int real_read_len;
src_file = open(SRC_FILE_NAME, O_RDONLY);
dest_file = open(DEST_FILE_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (src_file < 0 || dest_file < 0)
{
printf("Open file error\n");
exit(1);
}
lseek(src_file, -OFFSET, SEEK_END);
while ((real_read_len = read(src_file, buff, sizeof(buff))) > 0)
{
write(dest_file, buff, real_read_len);
}
close(dest_file);
close(src_file);
return 0;
}
源文件:
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat src_file
123456123456123456123456123456123456123456123456123456123456123456123456
编译运行结果:
ngnsvr9 [** NONE **]/home/xionghailong/example $ gcc copy_file.c -o copy_file
ngnsvr9 [** NONE **]/home/xionghailong/example $ ./copy_file
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat dest_file
123456123456123456123456123456123456123456123456123456123456123456123456
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat copy_file.c
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#define BUFFER_SIZE 1024
#define SRC_FILE_NAME "src_file"
#define DEST_FILE_NAME "dest_file"
#define OFFSET 10240
int main()
{
int src_file, dest_file;
unsigned char buff[BUFFER_SIZE];
int real_read_len;
src_file = open(SRC_FILE_NAME, O_RDONLY);
dest_file = open(DEST_FILE_NAME, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if (src_file < 0 || dest_file < 0)
{
printf("Open file error\n");
exit(1);
}
lseek(src_file, -OFFSET, SEEK_END);
while ((real_read_len = read(src_file, buff, sizeof(buff))) > 0)
{
write(dest_file, buff, real_read_len);
}
close(dest_file);
close(src_file);
return 0;
}
源文件:
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat src_file
123456123456123456123456123456123456123456123456123456123456123456123456
编译运行结果:
ngnsvr9 [** NONE **]/home/xionghailong/example $ gcc copy_file.c -o copy_file
ngnsvr9 [** NONE **]/home/xionghailong/example $ ./copy_file
ngnsvr9 [** NONE **]/home/xionghailong/example $ cat dest_file
123456123456123456123456123456123456123456123456123456123456123456123456
相关文章推荐
- /proc/* 代表的意义
- Linux从入门到精通(一):硬盘安装Ubuntu15.04与win7双系统
- 每天一个Linux命令-12(lsof)
- Linux文件系统
- linux下 gcc 默认搜索路径
- 六款常用的linux C/C++ IDE
- Linux电源管理(11)_Runtime PM之功能描述
- Linux电源管理(10)_autosleep
- Linux电源管理(9)_wakelocks
- Linux电源管理(8)_Wakeup count功能
- linux 命令 $1 $2 $@ $*等等含义
- Linux电源管理(7)_Wakeup events framework
- Linux电源管理(6)_Generic PM之Suspend功能
- Linux NTP配置详解 (Network Time Protocol)
- Linux 用户和组编程
- linux新增用户并增加sudo权限
- Linux电源管理(5)_Hibernate和Sleep功能介绍
- Linux电源管理(4)_Power Management Interface
- openstack usb透传研究分析( centos7 )
- Linux电源管理(3)_Generic PM之Reboot过程