Write a program to copy the content from one file to another file.

Rules to implement the above problem:
The input file should be named as "input.txt".
The output file should be named as "output.txt".
Sample Input file (input.txt):
Hello World!
Welcome to C Programming


Implementation of the above problem:
#include <stdio.h>
#include <stdlib.h> // For exit()
 int main()
{
FILE *fptr1, *fptr2;
char filename[100], c;
fptr1 = fopen("input.txt", "r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Open another file for writing
fptr2 = fopen("output.txt", "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}


Output file (output.txt)
Hello World!
Welcome to C Programming


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: