What code could I use to copy a file to another location on the computer?
Thanks in advance,
Will
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
Last edited by matsp; 02-09-2008 at 02:28 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.
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!):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 . . . .There's also MoveFile and MoveFileEx.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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();
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.Hehehe well, from a newbie's perspective, we are all "gods" who know everything.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.