?
Solved

Easy --- File Copy -- but urgent

Posted on 1998-07-16
9
Medium Priority
?
221 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 280 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

718 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