1 |
function theResult = fcopy(theSource, theDestination, maxCharacters) |
---|
2 |
|
---|
3 |
% fcopy -- Copy (duplicate) a file. |
---|
4 |
% fcopy(theSource, theDestination, maxCharacters) copies the |
---|
5 |
% contents of theSource file into theDestination file, |
---|
6 |
% in increments of maxCharacters (default = 16K). Each |
---|
7 |
% file can be specified by its name or by an existing |
---|
8 |
% file-pointer. |
---|
9 |
% fcopy (no arguments) demonstrates itself by copying |
---|
10 |
% "fcopy.m" to "junk.junk". |
---|
11 |
|
---|
12 |
% Copyright (C) 1997 Dr. Charles R. Denham, ZYDECO. |
---|
13 |
% All Rights Reserved. |
---|
14 |
% Disclosure without explicit written consent from the |
---|
15 |
% copyright owner does not constitute publication. |
---|
16 |
|
---|
17 |
if nargin < 1 |
---|
18 |
help fcopy |
---|
19 |
fcopy('fcopy.m', 'junk.junk'); |
---|
20 |
return |
---|
21 |
end |
---|
22 |
if nargin < 2, return, end |
---|
23 |
if nargin < 3, maxCharacters = 1024 .* 16; end |
---|
24 |
|
---|
25 |
if isstr(theSource) |
---|
26 |
src = fopen(theSource, 'r'); |
---|
27 |
if src < 0, error(' ## Source file not opened.'); end |
---|
28 |
else |
---|
29 |
src = theSource; |
---|
30 |
end |
---|
31 |
|
---|
32 |
if isstr(theDestination) |
---|
33 |
dst = fopen(theDestination, 'w'); |
---|
34 |
if dst < 0, error(' ## Destination file not opened.'); end |
---|
35 |
else |
---|
36 |
dst = theDestination; |
---|
37 |
end |
---|
38 |
|
---|
39 |
while (1) |
---|
40 |
[s, inputCount] = fread(src, [1 maxCharacters], 'char'); |
---|
41 |
if inputCount > 0, outputCount = fwrite(dst, s, 'char'); end |
---|
42 |
if inputCount < maxCharacters | outputCount < inputCount, break, end |
---|
43 |
end |
---|
44 |
|
---|
45 |
if isstr(theDestination), result = fclose(dst); end |
---|
46 |
if isstr(theSource), result = (fclose(src) | result); end |
---|
47 |
|
---|
48 |
if nargout > 0, theResult = result; end |
---|