我用下面的代码复制的文件
文件大小跟目标文件总差几百字节
但是占用空间都是一样的,这是为什么
有什么解决办法吗?谢谢 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);
文件大小跟目标文件总差几百字节
但是占用空间都是一样的,这是为什么
有什么解决办法吗?谢谢 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);
bb
while (!feof(fpr) && ftell(fpw)<total)
{
size_t readbyte = fread(a,1024,1,fpr);
fwrite(a,readbyte,1,fpw);
}
但是你的代码只能复制10K,我的文件有10M
#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);
}
CopyFile我会,我是在为分块复制文件作准备
如果非要使用c库,使用setbuf给FILE关联你自己开辟出的足够大的缓冲如果只是为了实现分块复制文件,那最好使用内存文件映射CreateFileMapping
现在的问题是10M的文件拷贝不完全
用CFile也会有这样的问题吗?