專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

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

作者:公平   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月14日   【字體:

前一段時(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<stdio.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/stat.h>

 /*操作的基本權(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!!!\n");
                    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!!!\n");
                    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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

/*
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       /*這說明說明復(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)閉窗口

相關(guān)文章