|
-
Mar 3rd, 2002, 06:32 PM
#1
Thread Starter
Fanatic Member
Copy function?
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?
Alcohol & calculus don't mix.
Never drink & derive.
-
Mar 4th, 2002, 12:03 AM
#2
Write your own:
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);
}
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 =).
Z.
-
Mar 4th, 2002, 10:18 AM
#3
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 4th, 2002, 07:32 PM
#4
Thread Starter
Fanatic Member
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.
Alcohol & calculus don't mix.
Never drink & derive.
-
Mar 4th, 2002, 08:13 PM
#5
You should be able to fifure it out...
Z.
-
Mar 5th, 2002, 06:42 AM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 5th, 2002, 09:58 AM
#7
It was midnight... thats my only excuse =).
Z.
-
Mar 7th, 2002, 07:31 PM
#8
Thread Starter
Fanatic Member
Ok, I tried writing this:
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);
}
Works fine, except it prints ÿ at the end of the new file. Where's it coming from and how can I fix it?
Alcohol & calculus don't mix.
Never drink & derive.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|