Solved

Easy --- File Copy -- but urgent

Posted on 1998-07-16
9
210 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

856 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