<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    拷貝文件的三種方法源代碼

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 (1) 使用ANSI C的庫函數
       
        可以使用ANSI C的以下幾個庫函數:   
        FILE *fopen( const char *filename, const char *mode );   
        int fclose( FILE *stream );
        size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
        size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );

       示例源碼如下:


    /*
        cpc.c
        use c library to copy file
    */

    #include <stdio.h>
    #include <errno.h>

    #define BUF_SIZE    256

    int main(int argc, char *argv[])
    {
        FILE *in_file, *out_file;
        char data[BUF_SIZE];
        size_t bytes_in, bytes_out;
        long len = 0;

        if ( argc != 3 )
        {
            printf("Usage: %s file1 file2\n", argv[0]);
            return 1;
        }

        if ( (in_file = fopen(argv[1], "rb")) == NULL )
        {
            perror(argv[1]);
            return 2;
        }
        if ( (out_file = fopen(argv[2], "wb")) == NULL )
        {
            perror(argv[2]);
            return 3;
        }


        while ( (bytes_in = fread(data, 1, BUF_SIZE, in_file)) > 0 )
        {
            bytes_out = fwrite(data, 1, bytes_in, out_file);
            if ( bytes_in != bytes_out )
            {
                perror("Fatal write error.\n");
                return 4;
            }
            len += bytes_out;
            printf("copying file .... %d bytes copy\n", len);
        }

        fclose(in_file);
        fclose(out_file);

        return 0;
    }

    2. 使用Windows 32 API 函數
       主要用到以下幾個函數:
       HANDLE CreateFile( LPCTSTR lpFileName,
                          DWORD dwDesiredAccess,
                          DWORD dwShareMode,
                          LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                          DWORD dwCreationDispostion ,
                          DWORD dwFlagsAndAttributes,
                          HANDLE hTemplateFile);
       BOOL ReadFile( HANDLE hFile,
                      LPVOID lpBuffer,
                      DWORD nNumberOfBytesToRead,
                      LPDWORD lpNumberOfBytesRead,
                      LPOVERLAPPED lpOverlapped);
       BOOL WriteFile( HANDLE hFile,
                       LPCVOID lpBuffer,
                       DWORD nNumberOfBytesToWrite,
                       LPDWORD lpNumberOfBytesWritten,
                       LPOVERLAPPED lpOverlapped);

       示例代碼如下:
      

    /*    
        cpw.c
        copy file, use windows functions
    */

    #include <windows.h>
    #include <stdio.h>

    #define    BUF_SIZE    256

    int main(int argc, LPTSTR argv[])
    {
        HANDLE hIn, hOut;
        DWORD dwIn, dwOut;
        TCHAR Data[BUF_SIZE];
        DWORD dwLen = 0;

        if ( argc != 3 )
        {
            printf("Usage : %s file1 file2\n", argv[0]);
            return 1;
        }

        hIn = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
        if ( INVALID_HANDLE_VALUE == hIn )
        {
            printf("Can't open open file %s : %x\n",
                argv[1], GetLastError());
            return 2;
        }

        hOut = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
            FILE_ATTRIBUTE_NORMAL, NULL);
        if ( INVALID_HANDLE_VALUE == hOut )
        {
            printf("Can't open file : %s: %x\n",
                argv[2], GetLastError());
            return 3;
        }

        while ( ReadFile(hIn, Data, BUF_SIZE, &dwIn, NULL) > 0)
        {
            WriteFile(hOut, Data, dwIn, &dwOut, NULL);
            if ( dwIn != dwOut )
            {
                printf("Fatal Error: %x\n", GetLastError());
                return 4;
            }
            dwLen += dwIn;
            printf("Copying file .... %d bytes copy\n", dwLen);
        }

        CloseHandle(hIn);
        CloseHandle(hOut);
        
        return 0;
    }

    3. 直接使用Windows的拷貝函數,實際上也是api函數,這個使用效率最高也最簡單

    BOOL CopyFile( LPCTSTR lpExistingFileName,
                   LPCTSTR lpNewFileName,
                   BOOL bFailIfExists );

    可是這個靈活性不高,但是使用上確是最簡單的示例代碼如下:

    /*
    cpwf.c
    copy file with windows function CopyFile
    */

    #include <windows.h>
    #include <stdio.h>

    int main(int argc, LPTSTR argv[] )
    {
        if ( argc != 3 )
        {
            printf("Usage %s file1 file2\n", argv[0]);
            return 1;
        }
        
        if ( !CopyFile(argv[1], argv[2], FALSE) )
        {
            printf("Copy file error : %x\n", GetLastError());
            return 2;
        }
        return 0;
    }

    前兩種方法都遵循以下的步驟
    1. 打開源文件, fopen或者CreateFile
    2. 打開目標文件, fopen 或者 CreateFile
    3. 讀文件,如果無誤,則將讀出的數據寫入到新文件,循環這個過程,直到文件結束
       用fread或者ReadFile讀,用fwrite或者WriteFile寫
    4. 關閉源文件和目標文件,fclose或者CloseHandle

    最后一種方法,用了一個Windows API函數就是CopyFile,實際上它就是把第一種或者第二種方法寫成了一個函數。
    這3種方法均在VC++6.0下測試通過。 第一種方法在linux下用gcc測試通過。

    其中第一種方法最具有通用性,由于使用了ANSI C標準庫函數,所以,可以不修改任何代碼在linux下編譯通過。RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全