ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 11, 2012
copy one file to another in c
/******************************************************************* * File : file-copy.c * Author : Saurabh Gupta * Description : copy one file to another in c * Date : PM 10:49 11 January 2012 * Source : http://www.ccplusplus.com/p/c.html *******************************************************************/ /* filecopy.c : Copy ccplusplus.com to prog.old */ #include <stdio.h> #include <stdlib.h> void main() { FILE *fp1, *fp2, *fopen(); int c ; fp1 = fopen( "ccplusplus.com", "r" ); /* open for reading */ fp2 = fopen( "prog.old", "w" ) ; /* open for writing */ if ( fp1 == NULL ) /* check does file exist etc */ { printf("Cannot open ccplusplus.com for reading \n" ); exit(1); /* terminate program */ } else if ( fp2 == NULL ) { printf("Cannot open prog.old for writing \n"); exit(1); /* terminate program */ } else /* both files O.K. */ { c = getc(fp1) ; while ( c != EOF) { putc( c, fp2); /* copy to prog.old */ c = getc( fp1 ) ; } fclose ( fp1 ); /* Now close files */ fclose ( fp2 ); printf("Files successfully copied \n"); } } /* * OUTPUT * [sgupta@rhel6x64 file-handling]$ gcc file-copy.c -o file-copy [sgupta@rhel6x64 file-handling]$ ./file-copy Files successfully copied [sgupta@rhel6x64 file-handling]$ cat ccplusplus.com XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX www.ccplusplus.com XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel6x64 file-handling]$ cat prog.old XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX www.ccplusplus.com XX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel6x64 file-handling]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment