ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 11, 2012
c program to copy contents of one file to another file
/******************************************************************* * File : file-copy.c * Author : Saurabh Gupta * Description : c program to copy contents of one file to another file * Date : PM 10:49 11 January 2012 * Source : http://www.ccplusplus.com/p/c.html *******************************************************************/ /* filecopy.c : Copy ccplusplus.com to prog.old */ #include
#include
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]$ */
1 comment:
g.p
October 2, 2012 at 5:53 PM
thanks
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
thanks
ReplyDelete