#include <stdio.h> #include <stdlib.h> // For exit() int main() { FILE *fptr1, *fptr2; char filename[100], c; printf("Enter the filename to open for reading \n"); scanf("%s", filename); // Open one file for reading fptr1 = fopen(filename, "r"); if (fptr1 == NULL) { printf("Cannot open file %s \n", filename); exit(0); } printf("Enter the filename to open for writing \n"); scanf("%s", filename); // Open another file for writing fptr2 = fopen(filename, "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; } |
chevron_right
filter_none
Output:
Enter the filename to open for reading a.txt Enter the filename to open for writing b.txt Contents copied to b.txt
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program to copy the contents of one array into another in the reverse order
- Write a C program that displays contents of a given file like 'more' utility in Linux
- C Program to print contents of file
- C Program to merge contents of two files into a third file
- C program to print odd line contents of a File followed by even line content
- C++ program to append content of one text file to another
- C program to copy string without using strcpy() function
- lseek() in C/C++ to read the alternate nth byte and write it in another file
- When is copy constructor called?
- When should we write our own copy constructor?
- Advanced C++ | Virtual Copy Constructor
- Copy elision in C++
- When does compiler create default and copy constructors in C++?
- Why copy constructor argument should be const in C++?
- Ways to copy a vector in C++
- C Program to count number of lines in a file
- C program to delete a file
- C++ program to print unique words in a file
- C Program for Lower Case to Uppercase and vice-versa in a file
- C++ program to read file word by word

