ccplusplus.com
Learn C, C++ Concepts
Tuesday, February 21, 2012
c program to write to a file
/******************************************************************* * File : file-copy.c * Author : Saurabh Gupta * Description : c program to write to a file * Date : 10:09 PM Tuesday, February 21, 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]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment