![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
how to copy files from one place to another in c
Hi,
I need to copy a log file to a new location called the old_logs directory. What function can I use in c to copy the file form "c:\log" to "c:\old_logs\log"? cheers, mechmind |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
There isn't a function, as such. You need to get a list of the files, open each file, read it into memory, write the data to the new file, and close it. You can then delete each file with the unlink function if you want.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
Using nothing but portable ISO C, the simplest useful program would be:
#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
FILE *in, *out;
int byte;
if ( argc < 3 ) {
fprintf ( stderr, "usage: prog <filefrom> <fileto>\n" );
return EXIT_FAILURE;
}
in = fopen ( argv[1], "r" );
if ( in == NULL ) {
fprintf ( stderr, "Error opening file\n" );
return EXIT_FAILURE;
}
out = fopen ( argv[2], "w" );
if ( out == NULL ) {
fprintf ( stderr, "Error opening file\n" );
return EXIT_FAILURE;
}
while ( ( byte = fgetc ( in ) ) != EOF )
fputc ( byte, out );
fclose ( in );
fclose ( out );
remove ( argv[1] );
return EXIT_SUCCESS;
}#include <stdio.h>
#include <stdlib.h>
int main ( int argc, char *argv[] )
{
char buffer[BUFSIZ];
if ( argc < 3 ) {
fprintf ( stderr, "usage: prog <filefrom> <fileto>\n" );
return EXIT_FAILURE;
}
sprintf ( buffer, "MOVE /Y %s %s", argv[1], argv[2] );
system ( buffer );
return EXIT_SUCCESS;
}unlink is not a portable function. remove is the standard C equivalent. |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Really? Never used it myself, but my C book says it works in Linux and Windows. Meh. You're the don.
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Feb 2005
Posts: 3
Rep Power: 0
![]() |
Great! Thanks for the help!
Regards, Mechmind |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
anyone have more info on how to use the remove function for C++...
thanx, BB98 |
|
|
|
|
|
#7 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4
![]() |
Quote:
Move files: If you are using MS-Windows operating system, you can use Win32 API function CopyFile(). |
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
The remove function.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
Well I guess I should have been more specific. You can give the function a file path, correct? So how does that work if the "\" symbol is an escape character to the compiler. (ie. "\n" for a new line.) How you you give C:\program files\adobe\acrobat 7.0\AcroRd32.exe for example. Does it have to be in double parens?
Sorry for being so stupid, just bought my C++ book last night, am trying to come up from C. thanx, BB98 [Edit] Thanx for the replies, starting to straighten out in my thick head.[\edit] |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Escape characters aren't a C++ thang. Escape the escape character. That would be "\\".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|