ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 11, 2012
copy one file to another entered by user in c
/******************************************************************* * File : copy.c * Author : Saurabh Gupta * Description : copy one file to another entered by user in c * Date : PM 11:10 11 January 2012 * Source : http://www.ccplusplus.com/p/c.html * Note : try to remove the gets *******************************************************************/ #include <stdio.h> #include <stdlib.h> void main() { FILE *fp1, *fp2, *fopen(); int c ; char fname1[40], fname2[40] ; printf("Enter source file:") ; gets(fname1); printf("Enter destination file:"); gets(fname2); fp1 = fopen( fname1, "r" ); /* open for reading */ fp2 = fopen( fname2, "w" ) ; /* open for writing */ if ( fp1 == NULL ) /* check does file exist etc */ { printf("Cannot open %s for reading \n", fname1 ); exit(1); /* terminate program */ } else if ( fp2 == NULL ) { printf("Cannot open %s for writing \n", fname2 ); exit(1); /* terminate program */ } else /* both files O.K. */ { c = getc(fp1) ; /* read from source */ while ( c != EOF) { putc( c, fp2); /* copy to destination */ c = getc( fp1 ) ; } fclose ( fp1 ); /* Now close files */ fclose ( fp2 ); printf("Files successfully copied \n"); } } /* * OUTPUT * [sgupta@rhel6x64 file-handling]$ gcc copy.c -o copy [sgupta@rhel6x64 file-handling]$ ./copy Enter source file:ccplusplus.com Enter destination file:prog.old 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