Link to home
Start Free TrialLog in
Avatar of SteveWood
SteveWood

asked on

Easy --- File Copy -- but urgent

Can anyone show me some code that will copy a file from one directory into another?
Avatar of saneesh100
saneesh100

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..
/* 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);
Err, those filenames actually meant pathnames.
Avatar of SteveWood

ASKER

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
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.
There you are!

What is the status on the strstr problem?

ASKER CERTIFIED SOLUTION
Avatar of chrisbill
chrisbill

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Cheers chrisbill this works a treat.