This is a simple C program that copies the contents of one ASCII (text) file onto another file. It is similar to Unix’s
cp
command.
This C program is called with two parameters i.e. the names of two files (fin and fout). The contents of the file referenced in second parameter are copied onto the file which is referenced by first parameter.
#include <stdio.h> 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); }