非常奇怪的问题,请高手关注 200分(200分)

C

clvin

Unregistered / Unconfirmed
GUEST, unregistred user!
var<br>&nbsp; FromF, ToF: file;<br>&nbsp; NumRead, NumWritten, bufsize: Integer;<br>&nbsp; buf: Pointer;<br>begin<br>&nbsp; bufsize := 10485760; //10M<br>&nbsp; buf := VirtualAlloc(nil, bufsize, MEM_COMMIT, PAGE_READWRITE);<br>&nbsp; AssignFile(FromF, Source);<br>&nbsp; Reset(FromF, 1); { Record size = 1 }<br>&nbsp; size := FileSize(FromF);<br>&nbsp; AssignFile(ToF, Destination); { Open output file }<br>&nbsp; Rewrite(ToF, 1); { Record size = 1 }<br>&nbsp; repeat<br>&nbsp; &nbsp; BlockRead(FromF, PData, bufsize, NumRead);<br>&nbsp; &nbsp; BlockWrite(ToF, PData, NumRead, NumWritten);<br>&nbsp; until (NumRead = 0) or (NumWritten &lt;&gt; NumRead) or (NumRead = size);<br>&nbsp; VirtualFree(buf, bufsize, MEM_RELEASE);<br>&nbsp; CloseFile(FromF);<br>&nbsp; CloseFile(ToF);<br>end;<br><br>上面是一段利用BlockRead, BlockWrite的文件拷贝代码,为了提高拷贝的速度,我用VirtualAlloc<br>得到了一个10M的buf来进行拷贝,文件可以正常拷贝完成,可是两个文件的内容总是在第0C,<br>0D, 0E, 0F处不同,也又可能只在0C,0D处不同,其他地方都一样。<br>我主要是想快速的复制文件,要是用其他方法可以实现也可以。<br>
 
使用WINDOWSAPI函数COPYFILE;
 
还可以用另一个API:SHFileOperation<br>
 
试试改成下面这样如何:<br>var<br>&nbsp; FromF, ToF: file of byte;<br>&nbsp; NumRead, NumWritten, bufsize: Integer;<br>&nbsp; buf: array[0..10485759] of byte;<br>begin<br>&nbsp; bufsize := 10485760; //10M<br>&nbsp; fillchar(buf,bufsize,#0);<br>// &nbsp;buf := VirtualAlloc(nil, bufsize, MEM_COMMIT, PAGE_READWRITE);<br>&nbsp; AssignFile(FromF, Source);<br>&nbsp; Reset(FromF, 1); { Record size = 1 }<br>&nbsp; size := FileSize(FromF);<br>&nbsp; AssignFile(ToF, Destination); { Open output file }<br>&nbsp; Rewrite(ToF, 1); { Record size = 1 }<br>&nbsp; repeat<br>&nbsp; &nbsp; BlockRead(FromF, buf, bufsize, NumRead);<br>&nbsp; &nbsp; BlockWrite(ToF, buf, NumRead, NumWritten);<br>&nbsp; until (NumRead = 0) or (NumWritten &lt;&gt; NumRead) or (NumRead = size);<br>// &nbsp;VirtualFree(buf, bufsize, MEM_RELEASE);<br>&nbsp; CloseFile(FromF);<br>&nbsp; CloseFile(ToF);<br>end;<br>buf是一个局部变量,当该程序执行完成后就自动施放,所以不用手工管理了.
 
to ieiszwxm, beta<br>我需要控制copy的过程,如现在copy的进度,如果失败了,下次还有继续的<br>to windbell<br>不能直接定义那么大的数组,即使用getmem也不能,我试过了
 
&nbsp; &nbsp;BlockRead(FromF, [red]PData[/red], bufsize, NumRead); //他们在那里定义的?<br>&nbsp; &nbsp; BlockWrite(ToF, [red]PData,[/red] NumRead, NumWritten);
 
to tseug<br>sorry 写错了,是buf
 
如果是buf, 你改称[red]Buf^[/red]试试。。。。<br>&nbsp; &nbsp; BlockRead(FromF, buf^, bufsize, NumRead); <br>&nbsp; &nbsp; BlockWrite(ToF, buf^, NumRead
 
提高拷贝速度也不用那么大的内存呀<br>100K的缓冲区和100M的缓冲区速度没有太大的区别<br>但分配大的内存可是很花时间的呀
 
to LiChaoHui<br>还有有些区别的不过2M以上区别就不大了,100k这些小内存相对还是比较忙的,尤其是copy比较<br>大的文件的时候,你可以试试,区别还是比较明显的。硬盘读写的综合速度大概6M/s,故我分配<br>了10M来做buf,以后还会慢慢调整的。
 
to tseug<br>大侠 谢谢,搞定了,居然是这个问题, ft.....<br>我调了好久,试了好多其他方法<br>分给你了,不过大家来的都有份<br>谢谢大家
 
多人接受答案了。
 
顶部