我用下面的代码复制的文件
文件大小跟目标文件总差几百字节
但是占用空间都是一样的,这是为什么
有什么解决办法吗?谢谢 CString strPath1 = "D:\\NOD32.RAR";
CString strPath2 = "D:\\NOD32_1.RAR"; FILE   *fpr,*fpw;     //fpr   源文件指针,fpw   目标文件指针 
char   a[1024];         //缓冲区   
long   readfile,total,KBCount=0,time_sp,count;    fpr = fopen(strPath1,"rb");
fpw = fopen(strPath2,"rb+");
if(!fpw)
{
fpw = fopen(strPath2,"wb");
}
fseek(fpr,0,SEEK_END);
total = ftell(fpr);
fseek(fpw,0,SEEK_END);
fseek(fpr,ftell(fpw),SEEK_SET);
readfile = ftell(fpr);
while (!feof(fpr) && ftell(fpw)<total) 
{
fread(a,1024,1,fpr);
fwrite(a,1024,1,fpw);
}
fclose(fpr);
fclose(fpw);

解决方案 »

  1.   

    fwrite(a,1024,1,fpw);最后一次应该是读成功了多少字节写多少字节,而不是写入1024字节
    bb
      

  2.   


    while (!feof(fpr) && ftell(fpw)<total)
    {
    size_t readbyte = fread(a,1024,1,fpr);
    fwrite(a,readbyte,1,fpw);
    }
      

  3.   

    谢谢WingForce(初六,履霜,坚冰至。)所给的代码
    但是你的代码只能复制10K,我的文件有10M
      

  4.   

    事实上你要复制文件,如果在windows下,最好用CopyFile函数:msdn上的例子:#include <windows.h>
    #include <stdio.h>void main()
    {
       WIN32_FIND_DATA FileData; 
       HANDLE hSearch; 
       DWORD dwAttrs;   
       TCHAR szDirPath[] = TEXT("c:\\TextRO\\"); 
       TCHAR szNewPath[MAX_PATH];   
     
       BOOL fFinished = FALSE; 
     
    // Create a new directory. 
     
       if (!CreateDirectory(szDirPath, NULL)) 
       { 
          printf("Could not create new directory.\n"); 
          return;
       } 
     
    // Start searching for text files in the current directory. 
     
       hSearch = FindFirstFile(TEXT("*.txt"), &FileData); 
       if (hSearch == INVALID_HANDLE_VALUE) 
       { 
          printf("No text files found.\n"); 
          return;
       } 
     
    // Copy each .TXT file to the new directory 
    // and change it to read only, if not already. 
     
       while (!fFinished) 
       { 
          lstrcpy(szNewPath, szDirPath); 
          lstrcat(szNewPath, FileData.cFileName); 
          if (CopyFile(FileData.cFileName, szNewPath, FALSE))
          { 
             dwAttrs = GetFileAttributes(FileData.cFileName); 
             if (dwAttrs==INVALID_FILE_ATTRIBUTES) return;          if (!(dwAttrs & FILE_ATTRIBUTE_READONLY)) 
             { 
                SetFileAttributes(szNewPath, 
                    dwAttrs | FILE_ATTRIBUTE_READONLY); 
             } 
          } 
          else 
          { 
             printf("Could not copy file.\n"); 
             return;
          } 
     
          if (!FindNextFile(hSearch, &FileData)) 
          {
             if (GetLastError() == ERROR_NO_MORE_FILES) 
             { 
                printf("Copied all text files.\n"); 
                fFinished = TRUE; 
             } 
             else 
             { 
                printf("Could not find next file.\n"); 
                return;
             } 
          }
       } 
     
    // Close the search handle. 
     
       FindClose(hSearch);
    }
      

  5.   

    谢谢楼上的
    CopyFile我会,我是在为分块复制文件作准备
      

  6.   

    不能复制大文件应该是由于缓冲不够导致的
    如果非要使用c库,使用setbuf给FILE关联你自己开辟出的足够大的缓冲如果只是为了实现分块复制文件,那最好使用内存文件映射CreateFileMapping
      

  7.   

    谢谢你的回复
    现在的问题是10M的文件拷贝不完全
    用CFile也会有这样的问题吗?
      

  8.   

    fwrite有大小限制,CFile可以解决这类问题