/* Filecopy.c (c) Kari Laitinen http://www.naturalprogramming.com/ 2013-11-28 File created. 2013-11-28 Last modification. */ #include int main() { FILE* file_to_be_copied ; FILE* new_duplicate_file ; char name_of_file_to_be_copied[ 20 ] ; char name_of_new_duplicate_file[ 20 ] ; char text_line_from_file[ 100 ] ; int text_line_counter = 0 ; printf( "\n This program copies text from one file" "\n to another file. Please, give the name of" "\n the file to be copied: " ) ; gets( name_of_file_to_be_copied ) ; printf( "\n Please, give the name of the duplicate file: " ) ; gets( name_of_new_duplicate_file ) ; file_to_be_copied = fopen( name_of_file_to_be_copied, "r" ) ; new_duplicate_file = fopen( name_of_new_duplicate_file, "w" ) ; if ( file_to_be_copied == 0 ) { printf( "\n Cannot open file %s\n", name_of_file_to_be_copied ) ; } else { if ( new_duplicate_file == 0 ) { printf( "\n Cannot open file %s\n", name_of_new_duplicate_file ) ; } else { printf( "\nCopying in progress ... \n" ) ; while( fgets( text_line_from_file, sizeof ( text_line_from_file ), file_to_be_copied ) != 0 ) { fputs( text_line_from_file, new_duplicate_file ) ; text_line_counter ++ ; } fclose( new_duplicate_file ) ; printf( "\nCopying ready. %d lines were copied. \n", text_line_counter ) ; } fclose( file_to_be_copied ) ; } }