1833828 Members
2347 Online
110063 Solutions
New Discussion

Re: duplicate in C

 
MR GROSS
Occasional Advisor

duplicate in C

Hello,

I want duplicate in C a file for example
"db.dhcp"
5 REPLIES 5
Volker Borowski
Honored Contributor

Re: duplicate in C

Hmm,
has been some time ago......

with "source" and "target" being filepointers:


source = fopen ( "db.dhcp", "rb" );
target = fopen ( "db.dhcp.copy", "wb" );

while ( !feof(source) )
{
fputc ( fgetc(source), target );
}
fclose(source);
fclose(target);

May be need to do a little polishing (i.e. errorhandling)
Volker
Robin Wakefield
Honored Contributor

Re: duplicate in C

Hi,

Something like:

=======================================
#include
#define FAIL 0
#define SUCCESS 1

int copyfile(char *infile, char *outfile)
{
FILE *fp1,*fp2;
int c;

if ((fp1 = fopen(infile, "r")) == NULL)
return FAIL;

if ((fp2 = fopen(outfile, "w")) == NULL) {
fclose(fp1);
return FAIL;
}

while ((c=getc(fp1)) != EOF)
putc(c, fp2);

fclose(fp1);
fclose(fp2);
return SUCCESS;
}
=====================================

should do it.

Rgds, Robin.
Deepak Extross
Honored Contributor

Re: duplicate in C

The purists among us will frown on this, but I've sneaked many a
system("cp ");
into my c code.

quick and dirty. note that this wont work on other pansy platforms - i'm assuming you're a Unix man.
harry d brown jr
Honored Contributor

Re: duplicate in C

#include

system("cp db.dhcp copyof_db.dhcp");


Almost a oneliner.


live free or die
harry
Live Free or Die
Deepak Extross
Honored Contributor

Re: duplicate in C

well??

did any of this help?