回 帖 发 新 帖 刷新版面

主题:多线程拷贝文件  有米有哪个好心人帮我在自己的机子上调下这个代码

#include "StdAfx.h"
#include "windows.h"
#include "stdio.h"
#include "tchar.h"
#define MAX_LEN 1000

typedef struct  
{
     HANDLE exitFileHandle;
     HANDLE copyFileHandle; 
    int n;                     //thread number
    DWORD exthread;              //executing thread number
    DWORD  persize;               // thread read the size of file  
 }CMPFILE,*lpCMPFILE;

 DWORD WINAPI copyfileFunc(PVOID paPram){ 
     CMPFILE *cmpfile = (lpCMPFILE) paPram;
     int str[MAX_LEN];
     str[cmpfile->exthread] = cmpfile->exthread;
     OVERLAPPED zread[MAX_LEN]={0};
     zread[str[cmpfile->exthread]].Offset=cmpfile->exthread*cmpfile->persize;
     DWORD readsize;
     char *buffer=new char[cmpfile->persize*cmpfile->n];
     HANDLE hEvents[MAX_LEN];
     hEvents[str[cmpfile->exthread]]=CreateEvent(NULL,TRUE,FALSE,NULL);
     zread[str[cmpfile->exthread]].hEvent = hEvents[str[cmpfile->exthread]];
      

       for(int j=0;j<cmpfile->n;j++)
    {
         BOOL rSuccess;
            Sleep(2000);     
         rSuccess=ReadFile(cmpfile->exitFileHandle,&buffer[str[cmpfile->exthread]],cmpfile->persize,&readsize,&zread[str[cmpfile->exthread]]);
         
            if (!rSuccess)
         {
                printf("Read file fail with error %d\n",GetLastError());
         }
            else
         { 
             printf("%s",buffer);
             BOOL wSuccess;
             DWORD writesize;
             wSuccess=WriteFile(cmpfile->copyFileHandle,&buffer[str[cmpfile->exthread]],cmpfile->persize,&writesize,&zread[str[cmpfile->exthread]]);
             if (!wSuccess)
             {
                 printf("Write file fail with error %d\n",GetLastError());
             }    
             CloseHandle(hEvents[j]);
         }    
       }     
     delete []buffer;
     
     return 0;
 }
void main(int argc,char *argv[]){
    HANDLE hFile;
    HANDLE hTmpFile;
    CMPFILE *cmpfile = new CMPFILE;
    if (argv[1]!=NULL&&argv[2]!=NULL)
    {
        char exitstr[MAX_LEN]={"0"};
        char copystr[MAX_LEN]={"0"};
        DWORD filesize=0;
        DWORD persize=0;
        int threadnum=0;
        int i;
        
        HANDLE fhandle[MAX_LEN];
        if ( argv[3]==NULL)
        {
                 threadnum=1;                                     
                 cmpfile->n=threadnum;
                 sprintf(exitstr,"f:\\%s",argv[1]);
                 sprintf(copystr,"f:\\%s",argv[2]);
                 hFile = CreateFileA(exitstr,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_FLAG_OVERLAPPED,NULL);
                 hTmpFile= CreateFileA(copystr,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_FLAG_OVERLAPPED,NULL);
                 
                 cmpfile->exitFileHandle = hFile;
                 cmpfile->copyFileHandle = hTmpFile;
                 filesize = GetFileSize(hFile,NULL);
                 persize = filesize/threadnum;
                 printf("%5d%5d\n",filesize,persize);

                 cmpfile->persize=persize;
                 for (i=0;i<threadnum;i++ )
                 {
                     cmpfile->exthread=i;
                     fhandle[i]= CreateThread( NULL,0,copyfileFunc,cmpfile,0,NULL);
                     WaitForSingleObject(fhandle[i],INFINITE);
                    
                 }
                 CloseHandle(hFile);
                 CloseHandle(hTmpFile);
                
                 
        }
        else
        {
           threadnum=atoi(argv[3]);
           cmpfile->n=threadnum;
           sprintf(exitstr,"C:\\%s",argv[1]);
           sprintf(copystr,"C:\\%s",argv[2]);
           hFile = CreateFileA(exitstr,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_FLAG_OVERLAPPED,NULL);
           hTmpFile= CreateFileA(copystr,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_FLAG_OVERLAPPED,NULL);
           
           cmpfile->exitFileHandle = hFile;
           cmpfile->copyFileHandle = hTmpFile;
           filesize = GetFileSize(hFile,NULL);
           persize = filesize/threadnum;
           printf("%5d%5d\n",filesize,persize);
           
           cmpfile->persize=persize;
           for (i=0;i<threadnum;i++ )
           {
               cmpfile->exthread=i;
               fhandle[i]= CreateThread( NULL,0,copyfileFunc,cmpfile,0,NULL);
               
           }
           WaitForMultipleObjects(threadnum, fhandle,TRUE, INFINITE);
           CloseHandle(fhandle);
           CloseHandle(hFile);
           CloseHandle(hTmpFile);

        }
    }
    else
    {
        printf("argv[1] or argv[2] is null");
    }
    delete []cmpfile;
}        

我实在是调不了了, 大侠帮我调调吧,拜托了

回复列表 (共1个回复)

沙发


#include "StdAfx.h"  注释掉

我来回复

您尚未登录,请登录后再回复。点此登录或注册