What code could I use to copy a file to another location on the computer?
Thanks in advance,
Will
Printable View
What code could I use to copy a file to another location on the computer?
Thanks in advance,
Will
Depends on what you want to achieve and on what OS, but the standard C way is to use two calls to fopen() to open the existing and create the new location file, and then fread()/fwrite() in a loop for as much as you need to, then fclose().
On specific systems, such as Windows, there are system functions to do this for you, but it locks you in with that OS.
--
Mats
Thanks. That's all I need to know. If you wanted to know, I have windows xp pro. And I was just asking the question for future knowledge.
For Windows API, CopyFile and CopyFileEx works. There's also MoveFile and MoveFileEx.
Thanks Elysia, at my stage, every piece of info counts.
In C++, of course, one would use file streams. Perhaps ifstream, ofstream, and the >> and << operators. Maybe something like this (untested!):Quote:
Depends on what you want to achieve and on what OS, but the standard C way is to use two calls to fopen() to open the existing and create the new location file, and then fread()/fwrite() in a loop for as much as you need to, then fclose().
Code:#include <fstream>
bool copy_file(const char *from, const char *to) {
std::ifstream in(from);
if(!in.is_open()) return false;
std::ofstream out(to);
if(!out.is_open()) return false;
char c;
while(in >> c) out << c;
return true;
}
There's also the standard C function rename() for that . . . .Quote:
There's also MoveFile and MoveFileEx.
Are you kidding? That solution is horrible :(
Reading and writing a char at a time will take forever.
If you're using the fstream classes you'd want to open the files in binary mode though, unless you know they're text files...
Of course the easiest and least portable way to do it is using the system() function. ;)
Thanks, all. And I'll keep this forum in my bookmarks! It's people are full of information.
The easiest way is this:
Where copy_from and copy_to are ifstream and ofstream, properly declared and opened. Using OS specific functions will be more optimal, however.Code:copy_to << copy_from.rdbuf();
I don't believe Elysia knows everything, but it's pretty obvious that the overhead of checking and reading every single character of the input file and writing each one individually to the output file will be slower than doing it with blocks. fstream supports a read & write function [respectively for ifstream and ofstream, commonly for fstream]. Reading blocks of 4KB or so will be reasonably efficient (even multiples of 2 to the power of n is also a good idea, as disk blocks are always even powers of 2). Bigger blocks may work better in some OS's & HW combos, but not at all guaranteed [and the benefit gets smaller and smaller as the blocks grow bigger, until you start loosing out again for really big blocks].
--
Mats
Sorry for the inefficient program . . . my C++ skills are such that I didn't feel confident enough to post a solution that used blocks instead of characters . . . now ask for a C version, and you might get something. ;)
I guess everyone is a newbie compared to Salem, then. :)Quote:
Hehehe well, from a newbie's perspective, we are all "gods" who know everything.