Solved

Easy --- File Copy -- but urgent

Posted on 1998-07-16
9
200 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
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
 
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

Will my email signature work in Office 365?

You've built an email signature using raw HTML code in Office 365, but you can't review how it looks with Transport Rules. So you have to test it over and over again before it can be used. Isn't this a bit of a waste of your time? Wouldn't a WYSIWYG editor make it a lot easier?

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 70
Socket Programming (Unix) 8 119
c++ getting the first 10 characters of a char* string 11 90
gdb doesn't stop on breakpoint 2 50
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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now