C Program to Copy Contents of One File into Another File

Copying Contents of One File into Another File in C

In C language, we can copy the contents of one file into another file using file handling functions. For this purpose, we use fgetc() to read characters from the source file and fputc() to write those characters into the destination file. In this program, we will copy the contents of a file named abc.txt into another file named xyz.txt. The source file is opened in read mode and the destination file is opened in write mode.

सी लैंग्वेज में File Handling Functions की सहायता से एक File के Content को दूसरी File में Copy किया जा सकता है। इसके लिए Source File से Characters पढ़ने के लिए fgetc() तथा Destination File में Characters लिखने के लिए fputc() Function का उपयोग किया जाता है। इस प्रोग्राम में हम abc.txt नाम की File के Contents को xyz.txt नाम की दूसरी File में Copy करेंगे। Source File को Read Mode में तथा Destination File को Write Mode में Open किया जाता है।

C Program

#include <stdio.h>
int main(void)
{
FILE *fp1, *fp2;
int ch;
int error = 0;
/* Open source file */
fp1 = fopen("abc.txt", "rb");
if (fp1 == NULL)
{
printf("Cannot open source file.\n");
return 1;
}
/* Open destination file */
fp2 = fopen("xyz.txt", "wb");
if (fp2 == NULL)
{
printf("Cannot create destination file.\n");
fclose(fp1);
return 1;
}
/* Copy contents from one file to another */
while ((ch = fgetc(fp1)) != EOF)
{
if (fputc(ch, fp2) == EOF)
{
error = 1;
break;
}
}
/* Check for reading error */
if (ferror(fp1))
{
error = 1;
}
fclose(fp1);
/* Check for writing or closing error */
if (fclose(fp2) != 0)
{
error = 1;
}
if (error)
{
printf("File copying failed.\n");
return 1;
}
printf("File copied successfully.\n");
return 0;
}

Input File: abc.txt

First, create a text file named abc.txt and write the following contents in it.

सबसे पहले abc.txt नाम की एक Text File बनाएं और उसमें निम्नलिखित Content लिखें।

C Programming
File Handling in C
Copying contents of a file

Save this file in the program's current working directory.

इस File को Program की Current Working Directory में Save करें।

Output

File copied successfully.

After successful execution, a new file named xyz.txt will be created in the same working directory.

प्रोग्राम सफलतापूर्वक Execute होने के बाद उसी Working Directory में xyz.txt नाम की एक नई File बन जाएगी।

Contents of xyz.txt:

C Programming
File Handling in C
Copying contents of a file

The contents of xyz.txt will be the same as the contents of abc.txt.

xyz.txt File का Content abc.txt File के Content के समान होगा।

Explanation

  1. FILE *fp1, *fp2; declares two file pointers. fp1 is used for the source file and fp2 is used for the destination file.

    यह दो File Pointers Declare करता है। fp1 Source File तथा fp2 Destination File को Refer करता है।

  2. fopen("abc.txt", "rb") opens the source file in binary read mode. यह Source File को Read करने के लिए Open करता है।

  3. fopen("xyz.txt", "wb") opens the destination file in binary write mode. यदि File पहले से मौजूद है, तो उसका पुराना Content हट जाता है और नया Content लिखा जाता है।

  4. fgetc(fp1) reads one character at a time from the source file. यह Source File से एक बार में एक Character Read करता है।

  5. fputc(ch, fp2) writes the character into the destination file. यह Read किए गए Character को Destination File में Write करता है।

  6. while ((ch = fgetc(fp1)) != EOF) continues reading characters until the end of the source file is reached. यह Loop Source File के सभी Characters को Read करता है और EOF मिलने पर समाप्त हो जाता है।

  7. ferror(fp1) checks whether an error occurred while reading the source file. यह Reading के दौरान किसी Error की जांच करता है।

  8. fclose(fp1) and fclose(fp2) close the source and destination files. ये दोनों Files को Close करते हैं।

Important Points

  1. fgetc() returns an integer, so the variable used to store its result should be of type int, not char. fgetc() Function का Return Value Store करने के लिए int Variable का उपयोग करना चाहिए ताकि EOF को सही तरीके से पहचान सकें।

  2. Always use while ((ch = fgetc(fp1)) != EOF) to read a file until its end. File के अंत तक सही तरीके से Read करने के लिए इस Condition का उपयोग करना चाहिए।

  3. The "rb" mode opens a file for binary reading, while "wb" opens a file for binary writing. Binary File Copy करने के लिए इन Modes का उपयोग किया जाता है।

  4. Opening a destination file with "wb" removes its previous contents if it already exists. इसलिए Destination File में पहले से मौजूद Content हट सकता है।

  5. Always check whether fopen() returns NULL before performing file operations. File Open करने से पहले NULL की जांच करना आवश्यक है।

  6. Always close opened files using fclose(). File Operations पूरी होने के बाद सभी Opened Files को Close करना चाहिए।

Note: In this program, binary modes (rb and wb) are used so that the program copies the file contents byte by byte. It can copy ordinary text files as well as binary files. The source and destination should be different files.

इस प्रोग्राम में Binary Modes (rb और wb) का उपयोग किया गया है, जिससे File का Content Byte by Byte Copy होता है। यह Program सामान्य Text Files के साथ-साथ Binary Files को भी Copy कर सकता है। Source और Destination Files अलग-अलग होनी चाहिए।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025