Chinaunix首页 | 论坛 | 博客
  • 博客访问: 11572
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 40
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-09 14:26
文章分类
文章存档

2016年(3)

我的朋友
最近访客

分类: C/C++

2016-09-25 00:22:35

(注:若转载本文请注明出处!)
对于初学者,学习文件操作参考作用,希望我的一些时间能给到大家一点帮助。
代码粗糙,但还是涉及到了很到指针移动操作,初学者可以参考,看代码,不多解释。
linux环境
假如有/home/hello.tar.gz 
#gcc filecp.c -o filecp
#./filecp /home/hello.tar.gz   
#cd /home/
#./filecp hello.tar.gz              //上面两条命令 ,一样将会有一个文件拷贝文件产生在/home/hello_copy.tar.gz
#./filecp hello.tar.gz hello.gz  //当前目录拷贝 和cp hello.tar.gz hello.gz 一样
#./filecp /home/hello.tar.gz  .
#./filecp /home/hello.tar.gz ./   //两条命令一样,将会把hello.tar.gz 拷贝到当前目录
#./filecp /home/hello.tar.gz ./world
#./filecp /home/hello.tar.gz .world   //两条命令一样,将会把hello.tar.gz拷贝到当前目录,但是名字变为world.tar.gz
#./filecp /home/hello.tar.gz ./world.gz
#./filecp /home/hello.tar.gz .world.gz  //两条命令一样,将会把hello.tar.gz拷贝到当前目录,但是名字变为world.gz

注:不支持目录拷贝!
/*
*filecp.c
*/
#include
#include
#include
#include
#include
#include
void main(int argc, char **argv)
{
    int fd_s, fd_d;
    char buf[512];
    char dstname[100];
    int cnt = 0;
    int i = 0, j = 0;
    char *ptr = NULL;
    int stepNum = 0;
    //1. open source file.
    fd_s = open(argv[1],O_RDONLY);
    //2. open destination file.
    if (argc <=1) {
        printf("usage: ./filecp srcfilename  dstfilename\n");
    }
    else if (argc <=2) {
        while(argv[1][i] != '\0') {
            dstname[i] = argv[1][i];
            i++;
        }
        dstname[i] = '\0';
        printf("dstname: %s\n",dstname);
        ptr = strchr(dstname, '.');
        if(ptr) {
            printf("ptr %x - dstname %x = %d\n",ptr,dstname, ptr-dstname); 
            stepNum = i - (ptr - dstname) + 1;
            printf("stepNUm: %d i : %d\n",stepNum,i);
   int n =  0;
   for (n=0; n<5; n++) {
   for (j=0; j             ptr[0] = '_';
            ptr[1] = 'c';
            ptr[2] = 'o';
            ptr[3] = 'p';
            ptr[4] = 'y';
            printf("real destname: %s \n",dstname);
        }
        else {
            printf("dstname ptr = NULL\n");
        }
        fd_d = open(dstname,O_RDWR|O_CREAT,0666);
    }
    else if (argc <=3) {
        i = 0;
        while(argv[1][i] != '\0') {
            dstname[i] = argv[1][i];
            i++;
        }
        dstname[i] = '\0';
        if ((('.' == argv[2][0]) && ('\0' == argv[2][1]))|| \
            (('.' == argv[2][0]) && ('/' == argv[2][1]) && \
            ('\0' == argv[2][2])) ){
           ptr = strchr(dstname, '.');
           char *tmp = NULL;
           tmp = ptr;
           while ('/' != tmp[0]) {
               tmp--;
           }
           i = 0;
           while('\0' != (++tmp)[0]) {
                   dstname[i] = tmp[0];
                   i++;
           }
           dstname[i] = '\0';
           fd_d = open(dstname,O_RDWR|O_CREAT,0666);
        }
        else { 
            if (!strchr(++argv[2], '.')) {
                i = 0;
                j = 0;
                --argv[2];
                while ('\0' != argv[2][i]) {
                    if (('.' != argv[2][i]) && ('/' != argv[2][i])) {
                        dstname[j] = argv[2][i]; 
       j++;
                    }
                    i ++;
                }
                dstname[j] = '\0';
                ptr = strchr(++argv[1], '.');
                while ('\0' != ptr[0]) {
                    dstname[j] = (ptr++)[0];
                    j++;
                }
                dstname[j] = '\0';
            }
            else {
                i = 0;
                j = 0;
                while (('\0' != argv[2][i])) {
                     if('/' != argv[2][i]) {
                         dstname[j] = argv[2][i];
                         j++;
                     }
                     i++;
                }
                dstname[j] = '\0';
            }
           fd_d = open(dstname,O_RDWR|O_CREAT,0666);
        }
    }
    //3. read source file data.
    while((cnt = read(fd_s, buf, 512)) > 0 ) {
    /*4. write data to target file.*/
     //  printf("cnt : %d\n",cnt);
        write(fd_d, buf, cnt);
    }
    //5. close file descriptor.
    close(fd_s);
    close(fd_d);
}

阅读(1107) | 评论(0) | 转发(0) |
0

上一篇:Tail Queue description and one simple example

下一篇:没有了

给主人留下些什么吧!~~