Is there a copy function in C++? Currently I am using system("copy a b") but this prints out "x file(s) copied" which I don't want. Any ideas?
Printable View
Is there a copy function in C++? Currently I am using system("copy a b") but this prints out "x file(s) copied" which I don't want. Any ideas?
Write your own:
Off the top of my head, but it should work. You could speed it up by grabbing perhaps 500k chuncks at a time, instead of a single byte, but Ill leave that up to you =).Code:int copy(char* f1, char* f2)
{
FILE* fp1 = NULL;
FILE* fp2 = NULL;
fp1 = fopen(f1, "w+b");
fp2 = fopen(f2, "wb");
unsigned long flen;
fseek(fp1, SEEK_END, 0);
flen = ftell(fp1);
char t;
unsigned long i = 0;
while(i <= flen)
{
fread(&t, 1, 1, fp1);
fwrite(&t, 1, 1, fp2);
}
fclose(fp1);
fclose(fp2);
}
Z.
WinAPI has CopyFile.
Zaei: Doesn't work, all I ended up with was a 3MB file :)
CornedBee: I'd prefer not to use the API. I am trying to not go platform-dependent.
You should be able to fifure it out...
Z.
The problem is that you're at the end of the file...
Add a rewind(fp1); before reading data.
And I'd open it in pure read mode (rb), not read/write.
It was midnight... thats my only excuse =).
Z.
Ok, I tried writing this:Works fine, except it prints ΓΏ at the end of the new file. Where's it coming from and how can I fix it?Code:void copy(const char* srcpath, const char* destpath)
{
FILE* src = fopen(srcpath, "rb");
FILE* dest = fopen(destpath, "wb");
char t;
while (!feof(src))
{
t = fgetc(src);
fputc(t, dest);
}
fclose(src);
fclose(dest);
}