zoukankan      html  css  js  c++  java
  • 拷贝文件的三种方法源代码

    (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 ", 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. ");
                return 4;
            }
            len += bytes_out;
            printf("copying file .... %d bytes copy ", 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 ", 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 ",
                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 ",
                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 ", GetLastError());
                return 4;
            }
            dwLen += dwIn;
            printf("Copying file .... %d bytes copy ", 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 ", argv[0]);
            return 1;
        }
        
        if ( !CopyFile(argv[1], argv[2], FALSE) )
        {
            printf("Copy file error : %x ", 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下编译通过。
  • 相关阅读:
    Android9.0/8.1/6.0 默认给系统 app 授予所有权限
    Git——处理Warning: CocoaPods is installed but broken. Skipping pod install. You appear to have CocoaPods installed but it is not working.
    Git——error: RPC failed; curl 56 LibreSSL SSL_read:报错处理
    Git——To install: sudo gem install cocoapods 报错处理
    Xcode——清除缓存相关文件
    vue —— 拉取远程仓库在本地开发
    vue —— mac中配置Node、NPM环境
    Flutter——常见报错处理
    Git——从githup上克隆下一个完整的项目操作步骤
    Git——rebase实际操作遇到的问题
  • 原文地址:https://www.cnblogs.com/lidabo/p/3324662.html
Copyright © 2011-2022 走看看