C Program to Copy a File Using Command Line Arguments
Command-line arguments can be used to specify the source and destination filenames when executing a C program. Using this technique, we can copy the contents of one file into another file without asking the user to enter filenames during program execution.
सी लैंग्वेज में कमांड लाइन आर्गुमेंट की सहायता से प्रोग्राम चलाते समय Source और Destination फाइल के नाम प्रदान किए जा सकते हैं। इस तकनीक का उपयोग करके हम यूजर से प्रोग्राम चलाते समय फाइल के नाम पूछे बिना एक फाइल का डेटा दूसरी फाइल में कॉपी कर सकते हैं।
C Program
The following program copies the contents of the source file into the destination file using command-line arguments.
निम्न प्रोग्राम कमांड लाइन आर्गुमेंट का उपयोग करके Source फाइल के सभी डेटा को Destination फाइल में कॉपी करता है।
#include <stdio.h>#include <string.h>int main(int argc, char *argv[]){FILE *fp1, *fp2;int ch;if (argc != 3){printf("Usage: copy source destination\n");return 1;}if (strcmp(argv[1], argv[2]) == 0){printf("Source and destination files must be different.\n");return 1;}fp1 = fopen(argv[1], "rb");if (fp1 == NULL){printf("Cannot open source file.\n");return 1;}fp2 = fopen(argv[2], "wb");if (fp2 == NULL){printf("Cannot open destination file.\n");fclose(fp1);return 1;}printf("Copying file...\n");while ((ch = fgetc(fp1)) != EOF){if (fputc(ch, fp2) == EOF){printf("Error while writing destination file.\n");fclose(fp1);fclose(fp2);return 1;}}if (ferror(fp1)){printf("Error while reading source file.\n");fclose(fp1);fclose(fp2);return 1;}if (fclose(fp1) == EOF){printf("Error while closing source file.\n");fclose(fp2);return 1;}if (fclose(fp2) == EOF){printf("Error while closing destination file.\n");return 1;}printf("File copied successfully.\n");return 0;}
Compile and Execute the Program
Save the program as copy.c and compile it using GCC:
gcc copy.c -o copyExecute the program by providing the source and destination filenames:
./copy source.txt destination.txtOn Windows Command Prompt, use:
copy.exe source.txt destination.txtOutput
Copying file...File copied successfully.
The contents of source.txt will now be copied into destination.txt.
source.txt की सभी सामग्री अब destination.txt में कॉपी हो जाएगी।
Explanation of the Program
argcchecks the total number of command-line arguments. The program name and two filenames make the total count equal to 3.argcकमांड लाइन आर्गुमेंट की कुल संख्या चेक करता है। प्रोग्राम के नाम और दो फाइल नामों सहित इसकी कुल संख्या 3 होती है।argv[1]stores the source filename, andargv[2]stores the destination filename.argv[1]में Source फाइल का नाम औरargv[2]में Destination फाइल का नाम होता है।fopen(argv[1], "rb")opens the source file in binary read mode.fopen(argv[1], "rb")Source फाइल को Binary Read Mode में खोलता है।fopen(argv[2], "wb")opens the destination file in binary write mode. If the destination file already exists, its previous contents are erased.fopen(argv[2], "wb")Destination फाइल को Binary Write Mode में खोलता है। यदि Destination फाइल पहले से मौजूद है, तो उसका पुराना डेटा मिट जाता है।fgetc()reads one byte at a time from the source file, andfputc()writes that byte into the destination file.fgetc()Source फाइल से एक बार में एक Byte पढ़ता है तथाfputc()उस Byte को Destination फाइल में लिखता है।fclose()closes both files after the copying operation.कॉपी करने की प्रक्रिया पूरी होने के बाद
fclose()दोनों फाइलों को बंद करता है।
Important Points
The program uses command-line arguments to receive filenames.
The source file must exist before execution.
The destination file is created automatically if it does not exist.
Binary modes
"rb"and"wb"allow copying both text and binary files.The source and destination filenames must be different.
The program checks file-opening, reading, writing and closing errors.
यह प्रोग्राम फाइल नाम प्राप्त करने के लिए Command Line Arguments का उपयोग करता है।
प्रोग्राम चलाने से पहले Source फाइल का मौजूद होना आवश्यक है।
यदि Destination फाइल मौजूद नहीं है, तो वह स्वतः बनाई जाती है।
"rb"और"wb"Binary Modes की सहायता से Text और Binary दोनों प्रकार की फाइल कॉपी की जा सकती हैं।Source और Destination फाइल के नाम अलग-अलग होने चाहिए।
प्रोग्राम फाइल खोलने, पढ़ने, लिखने और बंद करने से संबंधित Errors को चेक करता है।
Comments
Post a Comment