C and C++ Programming Resources

Custom Search

Copying one file into another

Similar to Unix’s cp command. This program will be called with two parameters, the names of two files. It copies the second to the first.


/*******************************************************
*     MYCPLUS Sample Code - http://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include

int main(int argc, char * argv[]){
FILE *fin, *fout;
char c;

if (argc!=3){
printf("Usage: %s fileout filein\n", argc);
exit(0);
}
if ((fin=fopen(argv[2],"r"))==NULL){
perror("fopen filein");
exit(0);
}
if ((fout=fopen(argv[1],"w"))==NULL){
perror("fopen fileout");
exit(0);
}

while ((c=getc(fin))!=EOF)
putc(c,fout);

fclose(fin);
fclose(fout);
}

Tags: , ,

There are No Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.