mycopyFile_linux下实现简单文件的复制
来源:互联网 发布:hello kitty主题软件 编辑:程序博客网 时间:2024/05/21 22:44
/*
*Author:
*Function: 实现简单文件的复制拷贝
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define BUFFERSIZE 1024
int main(int argc,char *argv[])
{
int fd_read;//指向需要读的文件的filedes
int fd_write;//指向需要写入的文件的filedes
int ret_read;//read函数的返回值
int ret_write;//write函数的返回值
ssize_t ret;//read函数返回值
char buf[BUFFERSIZE];//定义缓冲区
if(3 != argc)//命令行参数检测
{
printf("Usermsg:./copy <file a> <file b>\n");
exit (-1);
}
//只读的方式打开a文件,需要被拷贝的文件
fd_read = open(argv[1],O_RDONLY);
//返回值检查
if(fd_read < 0)
{
perror("open the file a:");
exit(-1);
}
//只读的方式打开b文件,需要拷贝的目标文件,设定权限为可读可写可执行
fd_write = open(argv[2],O_WRONLY | O_CREAT,0777);
//返回值检查
if(fd_write < 0)
{
perror("open the file b:");
exit(-1);
}
//进入循环读写操作
while((ret = read(fd_read,buf,BUFFERSIZE)) > 0)
{
if(write(fd_write,buf,ret) > 0)
{
continue;
}
else
{
break;
}
close(fd_read);
close(fd_write);
exit(0);
*Author:
*Function: 实现简单文件的复制拷贝
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define BUFFERSIZE 1024
int main(int argc,char *argv[])
{
int fd_read;//指向需要读的文件的filedes
int fd_write;//指向需要写入的文件的filedes
int ret_read;//read函数的返回值
int ret_write;//write函数的返回值
ssize_t ret;//read函数返回值
char buf[BUFFERSIZE];//定义缓冲区
if(3 != argc)//命令行参数检测
{
printf("Usermsg:./copy <file a> <file b>\n");
exit (-1);
}
//只读的方式打开a文件,需要被拷贝的文件
fd_read = open(argv[1],O_RDONLY);
//返回值检查
if(fd_read < 0)
{
perror("open the file a:");
exit(-1);
}
//只读的方式打开b文件,需要拷贝的目标文件,设定权限为可读可写可执行
fd_write = open(argv[2],O_WRONLY | O_CREAT,0777);
//返回值检查
if(fd_write < 0)
{
perror("open the file b:");
exit(-1);
}
//进入循环读写操作
while((ret = read(fd_read,buf,BUFFERSIZE)) > 0)
{
if(write(fd_write,buf,ret) > 0)
{
continue;
}
else
{
break;
}
}
close(fd_read);
close(fd_write);
exit(0);
}
测试结果:
使用编译命令进行编译:gcc -o copy mycopyFile.c
./copy a.txt b.txt
a.txt必须是存在的文件,b.txt没有关系。
我在a.txt中的内容是hello,结果在b.txt中也复制了内容,hello。j结果正确。
- mycopyFile_linux下实现简单文件的复制
- 文件复制命令CP 的简单实现
- java简单实现复制文件
- FileInputStream和FileOutputStream实现简单的文件复制
- 实现文件的复制
- 实现文件的复制
- Linux下如何实现两个IP机器的文件复制
- python实现将某类文件复制到特定的目录下
- Java实现复制一个目录下的所有文件
- 在linux下实现文件复制的功能
- 简单的文件复制类
- java简单的文件复制
- Java简单实现多线程复制文件
- 简单实现I/O文件复制
- 简单实现文件遍历、比较、复制功能
- Linux下C语言实现文件复制
- linux下C语言实现文件复制
- 一个简单的实现复制文件并修改文件扩展名的小程序
- 求霍夫曼扩展编码
- ios判断是否为数字(全拼数字)
- 数据结构中三表合一的实现
- 设计模式之禅笔记——工厂模式
- C/C++ 内存分配
- mycopyFile_linux下实现简单文件的复制
- cocos2dx 中通过 jni 显示UI
- qq,sina第3方登录js版
- INNODB 和 MYISAM 区别 事物处理
- Ubuntu eclipse :An error has occurred. See the log file
- The difference between the int value ++; and the value;
- C语言中printf格式化输出函数
- vs2010编译osg源码问题:error C2780
- mybatis学习整理(用户手册)(完整的全面学习)