且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

拷贝文件的三种方法源代码

更新时间:2021-08-12 05:17:16

(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下编译通过。