Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 3rd, 2005, 9:38 AM   #1
mechmind
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 mechmind is on a distinguished road
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
mechmind is offline   Reply With Quote
Old Feb 3rd, 2005, 11:15 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 3rd, 2005, 1:00 PM   #3
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
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;
}
However, calling out to the environment would be far simpler:
#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;
}
>You can then delete each file with the unlink function if you want.
unlink is not a portable function. remove is the standard C equivalent.
Eggbert is offline   Reply With Quote
Old Feb 3rd, 2005, 5:11 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Really? Never used it myself, but my C book says it works in Linux and Windows. Meh. You're the don.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 5th, 2005, 9:58 PM   #5
mechmind
Newbie
 
Join Date: Feb 2005
Posts: 3
Rep Power: 0 mechmind is on a distinguished road
Great! Thanks for the help!

Regards,
Mechmind
mechmind is offline   Reply With Quote
Old Jun 10th, 2005, 12:14 PM   #6
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
anyone have more info on how to use the remove function for C++...

thanx,
BB98
badbasser98 is offline   Reply With Quote
Old Jun 10th, 2005, 12:42 PM   #7
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by badbasser98
anyone have more info on how to use the remove function for C++...

thanx,
BB98
The remove() function?? there isn't anything to it. all it does is delete a file. You will find it described in any C or C++ book, www.msdn.microsoft.com or probably a lot of other places on the net (use google to find them). But its such a trival thing to use that it doesn't take but a few seconds to learn it.

Move files: If you are using MS-Windows operating system, you can use Win32 API function CopyFile().
Ancient Dragon is offline   Reply With Quote
Old Jun 10th, 2005, 12:47 PM   #8
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Jun 10th, 2005, 1:28 PM   #9
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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]
badbasser98 is offline   Reply With Quote
Old Jun 10th, 2005, 1:38 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You double-slash: C:\\Program Files\\Adobe\\Acrobat 7.0\\AcroRd32.exe
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC