如何实现模拟拷贝

claider 2000-08-13 06:13:00
我在vc++6中向编一个模仿dos拷贝的命令
用fread\fwrite 当度都可以
但用fread 读入缓冲区后
在以fwrite 写盘发生司机甚至重启
望高手指点
我用的控制台程序
...全文
150 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Kevin_qing 2000-08-16
  • 打赏
  • 举报
回复
怎么发了這麼多?
Kevin_qing 2000-08-16
  • 打赏
  • 举报
回复
看看这个函数,會不會对你有启发
注:AULPCSTR = LPCSTR
....
GFREE()=free()
GMALLOC=malloc()

int CopyFile( AULPCSTR lpExistingFileName, // pointer to name of an existing file
AULPCSTR lpNewFileName, // pointer to filename to copy to
int bFailIfExists // flag for operation if file exists /*always equal to 0 */
)

{
AULPSTR buffer;
AUULONG length;
AUULONG nWrite=0;
AUINT cWrite;
AUINT blkIn=0,blkOut=0;
FILE* fpIn;
FILE* fpOut;
assert(0==bFailIfExists);
fpIn=fopen(lpExistingFileName,"rb");
fpOut=fopen(lpNewFileName,"wb");
if(fpIn&&fpOut){
buffer=(AULPSTR)GMALLOC(4096);
fseek(fpIn,0,SEEK_END);
length=ftell(fpIn);
fseek(fpIn,0,SEEK_SET);
while(nWrite<length)
{
cWrite=length-nWrite>4096? 4096:length-nWrite;
blkIn+=fread(buffer,cWrite,1,fpIn);
blkOut+=fwrite(buffer,cWrite,1,fpOut);
nWrite+=cWrite;
}
GFREE(buffer);
fclose(fpIn);
fclose(fpOut);
return blkOut==blkIn;
}
fclose(fpIn);
fclose(fpOut);
return 0;

};
Kevin_qing 2000-08-16
  • 打赏
  • 举报
回复
看看这个函数,會不會对你有启发
注:AULPCSTR = LPCSTR
....
GFREE()=free()
GMALLOC=malloc()

int CopyFile( AULPCSTR lpExistingFileName, // pointer to name of an existing file
AULPCSTR lpNewFileName, // pointer to filename to copy to
int bFailIfExists // flag for operation if file exists /*always equal to 0 */
)

{
AULPSTR buffer;
AUULONG length;
AUULONG nWrite=0;
AUINT cWrite;
AUINT blkIn=0,blkOut=0;
FILE* fpIn;
FILE* fpOut;
assert(0==bFailIfExists);
fpIn=fopen(lpExistingFileName,"rb");
fpOut=fopen(lpNewFileName,"wb");
if(fpIn&&fpOut){
buffer=(AULPSTR)GMALLOC(4096);
fseek(fpIn,0,SEEK_END);
length=ftell(fpIn);
fseek(fpIn,0,SEEK_SET);
while(nWrite<length)
{
cWrite=length-nWrite>4096? 4096:length-nWrite;
blkIn+=fread(buffer,cWrite,1,fpIn);
blkOut+=fwrite(buffer,cWrite,1,fpOut);
nWrite+=cWrite;
}
GFREE(buffer);
fclose(fpIn);
fclose(fpOut);
return blkOut==blkIn;
}
fclose(fpIn);
fclose(fpOut);
return 0;

};
Kevin_qing 2000-08-16
  • 打赏
  • 举报
回复
看看这个函数,會不會对你有启发
注:AULPCSTR = LPCSTR
....
GFREE()=free()
GMALLOC=malloc()

int CopyFile( AULPCSTR lpExistingFileName, // pointer to name of an existing file
AULPCSTR lpNewFileName, // pointer to filename to copy to
int bFailIfExists // flag for operation if file exists /*always equal to 0 */
)

{
AULPSTR buffer;
AUULONG length;
AUULONG nWrite=0;
AUINT cWrite;
AUINT blkIn=0,blkOut=0;
FILE* fpIn;
FILE* fpOut;
assert(0==bFailIfExists);
fpIn=fopen(lpExistingFileName,"rb");
fpOut=fopen(lpNewFileName,"wb");
if(fpIn&&fpOut){
buffer=(AULPSTR)GMALLOC(4096);
fseek(fpIn,0,SEEK_END);
length=ftell(fpIn);
fseek(fpIn,0,SEEK_SET);
while(nWrite<length)
{
cWrite=length-nWrite>4096? 4096:length-nWrite;
blkIn+=fread(buffer,cWrite,1,fpIn);
blkOut+=fwrite(buffer,cWrite,1,fpOut);
nWrite+=cWrite;
}
GFREE(buffer);
fclose(fpIn);
fclose(fpOut);
return blkOut==blkIn;
}
fclose(fpIn);
fclose(fpOut);
return 0;

};
Kevin_qing 2000-08-16
  • 打赏
  • 举报
回复
给你一個函数看看

int CopyFile( LPCSTR lpExistingFileName, // pointer to name of an existing file
LPCSTR lpNewFileName, // pointer to filename to copy to
int bFailIfExists // flag for operation if file exists /*always equal to 0 */
)

{
LPSTR buffer;
ULONG length;
ULONG nWrite=0;
INT cWrite;
INT blkIn=0,blkOut=0;
FILE* fpIn;
FILE* fpOut;
assert(0==bFailIfExists);
fpIn=fopen(lpExistingFileName,"rb");
fpOut=fopen(lpNewFileName,"wb");
if(fpIn&&fpOut){
buffer=(LPSTR)malloc(4096);
fseek(fpIn,0,SEEK_END);
length=ftell(fpIn);
fseek(fpIn,0,SEEK_SET);
while(nWrite<length)
{
cWrite=length-nWrite>4096? 4096:length-nWrite;
blkIn+=fread(buffer,cWrite,1,fpIn);
blkOut+=fwrite(buffer,cWrite,1,fpOut);
nWrite+=cWrite;
}
free(buffer);
fclose(fpIn);
fclose(fpOut);
return blkOut==blkIn;
}
fclose(fpIn);
fclose(fpOut);
return 0;

};

看了有没有启发?
claider 2000-08-16
  • 打赏
  • 举报
回复
问题已经解决,我们的教材出了问题,将函数fread和fwrite的第二第三个参数印反
这本教材是
c语言设计教程第二版
高等教育出版社p315
但仍然谢谢诸位
分数已奉上
Maxwell 2000-08-15
  • 打赏
  • 举报
回复
在Win中不应使用流函数,可能会有问题.再一个fread后fwrite前用没用rewind.
U皮特U 2000-08-13
  • 打赏
  • 举报
回复
源码贴出来看看,一般来说是缓冲区溢出错误。

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧