Solved

Easy --- File Copy -- but urgent

Posted on 1998-07-16
9
220 Views
Last Modified: 2010-04-15
Can anyone show me some code that will copy a file from one directory into another?
0
Comment
Question by:SteveWood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 1

Expert Comment

by:saneesh100
ID: 1251737
In which OS do u want it ...
 in windows theer is an API call CopyFile which will do it for u
In dos  in TC u can use SYSTEM comand to do that,
please give more explanation to u'r question please..
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251738
/* Error handling omitted */
#include <stdio.h>

FILE *file1, *file2;
int c;

file1 = fopen(<source filename>, "r");
file2 = fopen(<dest filename>, "w");
while ((c = getc(file1)) != EOF)
  putc(c, file2);
fclose(file1);
fclose(file2);
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251739
Err, those filenames actually meant pathnames.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 1

Author Comment

by:SteveWood
ID: 1251740
mlev -- I Think this will only work for text files.  I need something that will copy any type of file.

Will be in a console app that needs to run on NT and 95/98.
I am using VC++ 5
0
 
LVL 1

Author Comment

by:SteveWood
ID: 1251741
If anyone can provide me with an example CopyFile program that might be usefull to me, I could see if that does the trick.

mlev - If your previous answer proves correct I will give you the points.  I can't test it as it won't compile on my machine.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1251742
There you are!

What is the status on the strstr problem?

0
 

Accepted Solution

by:
chrisbill earned 70 total points
ID: 1251743
// A program to copy a file
//

#include <windows.h>
#include <stdio.h>

int main( int argc, char *argv[] )
{
   if( argc != 3 ) {
      fprintf(stderr, "usage: MCP SRCFILE DESTFILE\n");
      return -1;
   }

   char *pSrcFile  = argv[1];
   char *pDestFile = argv[2];

   // If you want the copy to fail if the destination file
   //  exists, set the next bool to true.
   bool bFailIfFileExists = false;

   // Copy the file
   bool bResult = CopyFile(pSrcFile,pDestFile,bFailIfFileExists);

   // You can do some error handling here... (check bResult)
   //  That is, bResult=false if an error occurred.  If so,
   //  use GetLastError()
   return 1;
}

0
 
LVL 6

Expert Comment

by:alamo
ID: 1251744
mlev's example is as straightforward and simple as you can get, and should work or be very close. You mkight need to make the "r" and "w" into "rb" and "wb" to handle binary files, not certain which way it defaults. And of course you need to replace <source filename> with a path and filename string. Why doesn't it compile?
0
 
LVL 1

Author Comment

by:SteveWood
ID: 1251745
Cheers chrisbill this works a treat.  
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

634 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question