C program to copy contents of one file to another file
Last Updated :
19 Apr, 2024
[GFGTABS]
C
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
{
FILE *fptr1, *fptr2;
char filename[100];
int c;
printf("Enter the filename to open for reading: ");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(1);
}
printf("Enter the filename to open for writing: ");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s\n", filename);
exit(1);
}
// Read contents from file
while ((c = fgetc(fptr1)) != EOF)
{
fputc(c, fptr2);
}
printf("Contents copied to %s\n", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
[/GFGTABS]
Output:
Enter the filename to open for reading
a.txt
Enter the filename to open for writing
b.txt
Contents copied to b.txt
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...