Copies a single file. The source must exist, contain data and be readable. The target file will be overwritten it is already exists OR created as new if the target directory exists and the program the correct access permissions. Both the source and target must be fully specified and not contain wildcard characters.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | Filecopy | (hzString&,hzString&,) |
Declared in file: hzDirectory.h
Defined in file : hzDirectory.cpp
Function Logic:
Function body:
hzEcode Filecopy (hzString& tgt)hzString& src,
{
// Category: Directory
//
// Copies a single file. The source must exist, contain data and be readable. The target file will be overwritten it is already exists OR created as new if
// the target directory exists and the program the correct access permissions.
//
// Both the source and target must be fully specified and not contain wildcard characters.
//
// Arguments: 1) tgt The target file path.
// 2) src The source file path.
//
// Returns: E_ARGUMENT If either the source or the target filepath is NULL
// E_NOTFOUND If any filepath specified does not exist as a directory entry of any kind
// E_TYPE If any filepath names a directory entry that is not a file
// E_NODATA If the source file is empty
// E_OPENFAIL If the source file could not be opened for reading
// E_OK If the operation was successful
_hzfunc("Filecopy") ;
ifstream is ; // Input stream
ofstream os ; // Output stream
char buf [1028]; // Working buffer
hzEcode rc = E_OK ; // Return code
if (!tgt)
return hzerr(E_ARGUMENT, "No target filepath supplied") ;
// Open source for reading
rc = OpenInputStrm(is, src) ;
if (rc != E_OK)
return rc ;
// Open target for writing
os.open(tgt) ;
if (os.fail())
{
is.close() ;
return hzerr(E_WRITEFAIL, "Could not open/create target file %s\n", *tgt) ;
}
for (; rc == E_OK ;)
{
is.read(buf, 1024);
if (!is.gcount())
break ;
os.write(buf, is.gcount()) ;
if (os.fail())
return hzerr(E_WRITEFAIL, "Could not write %d bytes to target file %s\n", is.gcount(), *tgt) ;
}
is.close() ;
os.close() ;
threadLog("File %s copied to %s\n", *src, *tgt) ;
return rc ;
}