新聞中心

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

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

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

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

        本文引用地址:http://www.104case.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);
        }

        /*實現(xiàn)基本的文件內(nèi)容復(fù)制過程*/
        /*基本的思想就是將數(shù)據(jù)首先復(fù)制到一個大小合適的buf中,然后將buf其中的內(nèi)容寫到給文件2*/
        /*每一次都讀入buf中,然后寫buf的數(shù)據(jù)到文件2中*/
        char buf[1024];/*buf大小*/
        char *p = buf;/*指向buf的指針*/
        /*長度統(tǒng)計*/
        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;/*檢測還有多少沒有被復(fù)制*/
        p += wlength;/*移動指針到?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ù)制成功了,在文件中實現(xiàn)了數(shù)據(jù)的復(fù)制*/
        ....


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



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

        評論


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

        關(guān)閉
        主站蜘蛛池模板: 榆树市| 镇江市| 同心县| 高青县| 高邑县| 兴文县| 张家口市| 西藏| 依兰县| 鹰潭市| 炎陵县| 万山特区| 塔河县| 宁城县| 嘉鱼县| 乐业县| 施甸县| 财经| 栾城县| 哈巴河县| 长治市| 新宾| 财经| 革吉县| 阿坝县| 南宁市| 鹤壁市| 广宗县| 涪陵区| 买车| 新乡县| 井陉县| 东山县| 鲜城| 汝阳县| 社会| 交口县| 稷山县| 弋阳县| 汤阴县| 湘潭市|