Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1619658
  • 博文数量: 441
  • 博客积分: 20087
  • 博客等级: 上将
  • 技术积分: 3562
  • 用 户 组: 普通用户
  • 注册时间: 2006-06-19 15:35
文章分类

全部博文(441)

文章存档

2014年(1)

2012年(1)

2011年(8)

2010年(16)

2009年(15)

2008年(152)

2007年(178)

2006年(70)

分类: C/C++

2007-10-17 10:24:44

(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
#include

#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
#include

#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
阅读(1211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~