good day and a happy new year to you folks. im trying to copy the contents of one text file to another text file but i cant seem to get it right. written below is the code im using but it does not seem to work. any help please?
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main (void){
static const char filename[] = "file.txt";
static const char file2name[] = "file2.txt";
FILE *read = fopen ( filename, "r" );
FILE *write;
char line [128];
if ( read != NULL ){
while ( fgets ( line, sizeof line, read ) != NULL ){
write = fopen (file2name , "w");
fprintf (write , line);
fprintf (write , "\n");
fclose (write);
}
fclose ( read );
}
else{
perror ( filename ); /* why didn't the file open? */
}
getch();
return 0;
}