檔案複製:C語言方法、Windows API的兩種方法

來源:互聯網
上載者:User

標籤:

 

#include <stdio.h>#include <errno.h>#define BUF_SIZE 256int main(int argc, char *argv[]){    FILE *inFile, *outFile;    char rec[BUF_SIZE];    size_t bytesIn, bytesOut;    if(argc != 3)    {        printf("Usage:cpc file1 file2\n");        return 1;    }    inFile = fopen(argv[1],"rb");    if(inFile == NULL)    {        perror(argv[1]);        return 2;    }    outFile = fopen(argv[2],"wb");    if(outFile == NULL)    {        perror(argv[2]);        return 3;    }    while((bytesIn = fread(rec,1, BUF_SIZE,inFile))>0)    {        bytesOut = fwrite(rec, 1, bytesIn,outFile);        if(bytesIn != bytesOut)        {            perror("Fatal write error.");            return 4;        }    }    fclose(inFile);    fclose(outFile);    return 0;}
#include <Windows.h>#include <stdio.h>#define BUF_SIZE 256int main(int argc, LPTSTR argv[]){    HANDLE hIn, hOut;    DWORD nIn, nOut;    CHAR buffer[BUF_SIZE];    if(argc != 3)    {        printf("Usage:cpw file1 file2\n");        return 1;    }    hIn = CreateFile(argv[1],GENERIC_READ, FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);    if(hIn == INVALID_HANDLE_VALUE)    {        printf("Canno open input file. Error:%x\n",GetLastError());        return 2;    }    hOut = CreateFile(argv[2],GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL);    if(hOut == INVALID_HANDLE_VALUE)    {        printf("Cannot open output file.Error:%x\n",GetLastError());        return 3;    }    while(ReadFile(hIn, buffer, BUF_SIZE,&nIn, NULL) && nIn > 0)    {        WriteFile(hOut, buffer, nIn,&nOut,NULL);        if(nIn != nOut)        {            printf("Fatal write error:%x\n",GetLastError());            return 4;        }    }    CloseHandle(hIn);    CloseHandle(hOut);    return 0;}
#include <Windows.h>#include <stdio.h>int main(int argc, LPTSTR argv[]){    if(argc != 3)    {        printf("Usage:cpcf file1 file2\n");        return 1;    }    if(!CopyFile(argv[1], argv[2],FALSE))    {        printf("CopyFile Error:%x\n",GetLastError());        return 2;    }    return 0;}

 

檔案複製:C語言方法、Windows API的兩種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.