Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1861470
  • 博文数量: 152
  • 博客积分: 3730
  • 博客等级: 上尉
  • 技术积分: 3710
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:36
个人简介

减肥,运动,学习,进步...

文章分类

全部博文(152)

文章存档

2016年(14)

2015年(17)

2014年(16)

2013年(4)

2012年(66)

2011年(35)

分类: LINUX

2011-11-01 14:09:17

前一段时间采用mmap实现了一个文件的复制操作,现在采用linux的I/O实现文件的复制,基本的思想是将文件复制到一个通用的buf中,然后将buf中的数据复制到新的文件中。这种方式比较容易理解,但是也是底层的系统调用,建议少用,但有时候必须采用(设备驱动)。

  1. #include<stdio.h>
  2. #include
  3. #include
  4. #include<stdlib.h>
  5. #include
  6. #include
 /*操作的基本权限*/
  1. #define DEF_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH

  2. int main()
  3. {
  4.         /*源文件*/
  5.         int fdsrc = open("/home/gong/program/cprogram/Test.c",O_RDONLY,0);
  6.         if(fdsrc==-1)
  7.         {
  8.                 printf("error!!!\n");
  9.                 exit(-1);
  10.         }

  11.         /*获得源文件的大小*/
  12.         struct stat statbuf;
  13.         fstat(fdsrc,&statbuf);
  14.         int length = statbuf.st_size;

  15.         /*目的文件*/
  16.         int fddst = open("/home/gong/program/cprogram/copydata.c",O_CREAT|O_RDWR,DEF_MODE);
  17.         if(fddst==-1)
  18.         {
  19.                 printf("error!!!\n");
  20.                 exit(-1);
  21.         }
  22.         
  23.         /*实现基本的文件内容复制过程*/
  24.         /*基本的思想就是将数据首先复制到一个大小合适的buf中,然后将buf其中的内容写到给文件2*/
  25. /*每一次都读入buf中,然后写buf的数据到文件2中*/
  26.         char buf[1024];/*buf大小*/
  27.         char *p = buf;/*指向buf的指针*/
  28.         /*长度统计*/
  29.         int rlength=0,Rlength = 0;                                                        
  30.         int wlength=0;

  31.         while(length > 0)
  32.         {
  33.                 /*先读后先的过程*/
  34.                 rlength = read(fdsrc,p,1024);
  35.                 Rlength = rlength;
  36.                 while(rlength > 0)/*确保每次读出来的全部写入到文件中*/
  37.                 {
  38.                         wlength = write(fddst,p,rlength);
  39.                         rlength -= wlength;/*检测还有多少没有被复制*/
  40.                         p += wlength;/*移动指针到没有写入的区域*/
  41.                 }
  42.                 p = buf;/*确保每次都是复制到buf中*/
  43.                 length -= Rlength;
  44.         }

  45.         /*关闭文件*/
  46.         close(fdsrc);
  47.         close(fddst);
  48.         exit(0);
  49. }
编译调试:
[gong@Gong-Computer cprogram]$ gcc -g copyFile.c -o copyFile
[gong@Gong-Computer cprogram]$ ./copyFile 
/*Test.c*/
#include
#include
#include
#include

/*
char * returnstring(char *string)
{
        //static char buffer[1024];
        
        char * buffer = (char *)malloc(1024);
        strcpy(buffer,string);

        return buffer;
}*/

typedef int (*array10)[10];/*array100可以作为 int a[100]的指针*/
int main()
{
        /*
        int a = 10;
        int *p = &a;

        long int  apple = 0;
        apple = sizeof(int) * p;

        printf("The Number of apple is %ld\n",apple);
        */

        int a[10];
        int i = 0;
        for(i = 0;i<10;++i)
        {
                a[i] = i;
        }

        array10 b = a;
"copydata.c" 47L, 618C       /*这说明说明复制成功了,在文件中实现了数据的复制*/    
....       




以上的代码说明了基本实现了文件的复制,后期将采用标准I/O实现文件的复制。需要注意的是Linux I/O主要是返回了完成的数据量,这个可以方便操作。文件的复制过程是一个常用的过程。但是操作的方式存在一些差别,具体问题具体分析。                                      
阅读(3790) | 评论(0) | 转发(4) |
给主人留下些什么吧!~~