读书人

各位老大请问一个初学者有关问题

发布时间: 2012-03-25 20:55:17 作者: rapoo

各位老大,请教一个菜鸟问题~
如何用C++不调用系统函数写一个类式DOS下面的XCOPY的函数啊?希望给我点思路上的详细一点点的提示哈,谢谢大家~~

[解决办法]
拷贝单个文件无非是读出来再写进去
拷贝整个目录不过是递规搜索源目录再加拷贝单个文件
[解决办法]
比如:
long handle;
struct _finddata_t filestruct;  
char path_search[_MAX_PATH];
handle = _findfirst( "目录 ",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '. ' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
}
else
{
if( !stricmp(filestruct.name, szFilename) )
{
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
  if( ::GetFileAttributes(filestruct.name) &FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '. ')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}

[解决办法]
int copyfile(char *source, char *target)
{
char far *buf = NULL;
char prompt[] = "Target exists. Overwrite? ", newline[] = "\n\r ";
int hsource, htarget, ch;
unsigned ret, segbuf, count;

_dos_allocmem(0xffff, &segbuf);
count = segbuf;
if (ret = _dos_allocmem(count, &segbuf))
return ret;

FP_SEG(buf) = segbuf;

if (ret)
return ret;

if (pflag)
{
printf( "Do you want to copy %s? ", strupr(source));
ch = bdos(1, 0, 0) & 0x00ff;
printf( "\n ");
if ((ch == 'y ') || (ch == 'Y '))
{
// Open source file and create target, overwriting if necessary.
if (ret = _dos_open(source, O_RDONLY, &hsource))
return ret;

ret = _dos_creatnew(target, NORMAL, &htarget);
if (ret == EXIST)
{
if (yflag)
goto resume;
else
_dos_write(1, prompt, sizeof(prompt) - 1, &ch);
resume:
ch = bdos(1, 0, 0) & 0x00ff;
if ((ch == 'y ') || (ch == 'Y '))
ret = _dos_creat(target, NORMAL, &htarget);

_dos_write(1, newline, sizeof(newline) - 1, &ch);
} // end if.

// Read and write until there is nothing left.
while (count)
{
// Read and write input.
if ((ret = _dos_read(hsource, buf, count, &count)))
return ret;

if ((ret = _dos_write(htarget, buf, count, &count)))
return ret;

} // end while.

} // end if.
else


{
count++;
printf( "%s not added\n ", strupr(source));
exit(1);
} // end else.

} // end if.

// Open source file and create target, overwriting if necessary.
if (ret = _dos_open(source, O_RDONLY, &hsource))
return ret;

ret = _dos_creatnew(target, NORMAL, &htarget);
if (ret == EXIST)
{
_dos_write(1, prompt, sizeof(prompt) - 1, &ch);
ch = bdos(1, 0, 0) & 0x00ff;
if ((ch == 'y ') || (ch == 'Y '))
ret = _dos_creat(target, NORMAL, &htarget);

_dos_write(1, newline, sizeof(newline) - 1, &ch);
} // end if.

// Read and write until there is nothing left.
while (count)
{
// Read and write input.
if ((ret = _dos_read(hsource, buf, count, &count)))
return ret;

if ((ret = _dos_write(htarget, buf, count, &count)))
return ret;

if (vflag)
setverify(1);

} // end while.

// Close files and free memory.
_dos_close(hsource);
_dos_close(htarget);
_dos_freemem(segbuf);
return 0;

} // end copyfile.

读书人网 >C++

热点推荐