<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Linux I/O實(shí)現(xiàn)文件復(fù)制

          Linux I/O實(shí)現(xiàn)文件復(fù)制

          作者: 時(shí)間:2016-12-01 來源:網(wǎng)絡(luò) 收藏
          前一段時(shí)間采用mmap實(shí)現(xiàn)了一個(gè)文件的復(fù)制操作,現(xiàn)在采用linux的I/O實(shí)現(xiàn)文件的復(fù)制,基本的思想是將文件復(fù)制到一個(gè)通用的buf中,然后將buf中的數(shù)據(jù)復(fù)制到新的文件中。這種方式比較容易理解,但是也是底層的系統(tǒng)調(diào)用,建議少用,但有時(shí)候必須采用(設(shè)備驅(qū)動(dòng))。

          #include
          #include
          #include
          #include
          #include
          #include

          本文引用地址:http://www.ex-cimer.com/article/201612/324557.htm

          /*操作的基本權(quán)限*/

          #define DEF_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH

          int main()
          {
          /*源文件*/
          int fdsrc = open("/home/gong/program/cprogram/Test.c",O_RDONLY,0);
          if(fdsrc==-1)
          {
          printf("error!!!");
          exit(-1);
          }

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

          /*目的文件*/
          int fddst = open("/home/gong/program/cprogram/copydata.c",O_CREAT|O_RDWR,DEF_MODE);
          if(fddst==-1)
          {
          printf("error!!!");
          exit(-1);
          }

          /*實(shí)現(xiàn)基本的文件內(nèi)容復(fù)制過程*/
          /*基本的思想就是將數(shù)據(jù)首先復(fù)制到一個(gè)大小合適的buf中,然后將buf其中的內(nèi)容寫到給文件2*/
          /*每一次都讀入buf中,然后寫buf的數(shù)據(jù)到文件2中*/
          char buf[1024];/*buf大小*/
          char *p = buf;/*指向buf的指針*/
          /*長(zhǎng)度統(tǒng)計(jì)*/
          int rlength=0,Rlength = 0;
          int wlength=0;

          while(length > 0)
          {
          /*先讀后先的過程*/
          rlength = read(fdsrc,p,1024);
          Rlength = rlength;
          while(rlength > 0)/*確保每次讀出來的全部寫入到文件中*/
          {
          wlength = write(fddst,p,rlength);
          rlength -= wlength;/*檢測(cè)還有多少?zèng)]有被復(fù)制*/
          p += wlength;/*移動(dòng)指針到?jīng)]有寫入的區(qū)域*/
          }
          p = buf;/*確保每次都是復(fù)制到buf中*/
          length -= Rlength;
          }

          /*關(guān)閉文件*/
          close(fdsrc);
          close(fddst);
          exit(0);
          }

          編譯調(diào)試:
          [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",apple);
          */

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

          array10 b = a;
          "copydata.c" 47L, 618C /*這說明說明復(fù)制成功了,在文件中實(shí)現(xiàn)了數(shù)據(jù)的復(fù)制*/
          ....


          以上的代碼說明了基本實(shí)現(xiàn)了文件的復(fù)制,后期將采用標(biāo)準(zhǔn)I/O實(shí)現(xiàn)文件的復(fù)制。需要注意的是Linux I/O主要是返回了完成的數(shù)據(jù)量,這個(gè)可以方便操作。文件的復(fù)制過程是一個(gè)常用的過程。但是操作的方式存在一些差別,具體問題具體分析。



          關(guān)鍵詞: LinuxIO文件復(fù)

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();