- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program for copying the contents of one file into another file
File is a collection of records (or) is a place on hard disk, where data is stored permanently. By using C commands, we can access the files in different ways.
Operations on files
The operations that can be carried out on files in C language are as follows −
- Naming the file.
- Opening the file.
- Reading from the file.
- Writing into the file.
- Closing the file.
Syntax
The syntax for opening and naming file is as follows −
FILE *File pointer;
For example, FILE * fptr;
File pointer = fopen ("File name”, "mode”);
For example, fptr = fopen ("sample.txt”, "r”);
FILE *fp; fp = fopen ("sample.txt”, "w”);
The syntax for reading from file is as follows −
int fgetc( FILE * fp );// read a single character from a file
The syntax for writing into file is as follows −
int fputc( int c, FILE *fp ); // write individual characters to a stream
With the help of these functions, we can copy the content of one file into another file.
Example
Following is the C Program for copying the contents of one file into another file −
#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
"); scanf("%s",filename); // Open one file for reading fptr1 = fopen(filename, "r"); if (fptr1 == NULL){ printf("Cannot open file %s
", filename); exit(0); } 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
", filename); exit(0); } // Read contents from file c = fgetc(fptr1); while (c != EOF){ fputc(c, fptr2); c = fgetc(fptr1); } printf("
Contents copied to %s", filename); fclose(fptr1); fclose(fptr2); return 0; }
Output
When the above program is executed, it produces the following result −
Enter the filename to open for reading file3.txt Enter the filename to open for writing file1.txt Contents copied to file1.txt
- Related Articles
- C program to copy the contents of one file to another file?
- C# Program to read contents of a file into a string at once
- C++ program to append content of one text file to another
- Merge contents of two files into a third file using C
- Golang Program to Read the Contents of a File
- Print contents of a file in C
- Copying a File in java
- Copying file using FileStreams in Java
- Print array of strings in sorted order without copying one string into another in C++
- Java Program to Create String from Contents of a File
- How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?
- C program for file Transfer using UDP?
- How to read data from one file and print to another file in Java?
- How can I make one Python file run another?
- C# Program to read all the lines one by one in a file

Advertisements