Nov 3, 2012 at 1:08pm UTC
EOF may be a valid character in a binary file.
Nov 9, 2012 at 9:29am UTC
What if, the dest file, already contains something written in it..
Those char stil are left in the dest file after copying.. :(
Nov 9, 2012 at 10:35am UTC
Thanks. That solution executes considerably faster, as well as being straightforward.
Nov 9, 2012 at 12:06pm UTC
Another version I've been trying out. The size of the buffer may be system dependent. It can be optimised for faster execution.
2 3 4 5 6 7 8 9 10 11 12 13 void filecopy(FILE *dest, FILE *src)
const int size = 16384;
int n = fread(buffer, 1, size, src);
fwrite(buffer, 1, n, dest);
Last edited on
Nov 9, 2012 at 2:10pm UTC
Nov 10, 2012 at 6:20am UTC
I've opened the file using fopen() in the "w+" mode
Nov 10, 2012 at 7:58am UTC
The '+' implies you want to read from the file as well as write to it. Not sure why you'd want to do that. Also, with that mode specifier the file is opened as text mode.
For a file copy, binary mode is better.
2 3 4 5 6 7 8 9 10 void fileopen_and_copy( char * dest, char * src)
FILE * infile = fopen(src, "rb");
FILE * outfile = fopen(dest, "wb");
I see on the reference page linked above, "
On some library implementations, opening or creating a text file with update mode may treat the stream instead as a binary file.
Perhaps that is the case for your compiler. However, I'd still recommend the use of the '
' flag for binary mode instead.
One more comment. Your original post included statements for file rewind. But if all you ever do is simply open, copy and close, there is no need for rewind.
Last edited on
Nov 10, 2012 at 10:07am UTC
Nov 10, 2012 at 2:58pm UTC
Thanks all!! :)
Everything worked :)