The document describes a C program that copies one file to another using command line arguments. The program includes necessary headers, defines a main function that takes command line arguments, checks for valid arguments, opens the source and destination files, copies the source file contents to the destination file using a while loop, closes the files, and returns an error code or success message. Pseudocode and sample source code are provided to demonstrate the file copy algorithm.
The document describes a C program that copies one file to another using command line arguments. The program includes necessary headers, defines a main function that takes command line arguments, checks for valid arguments, opens the source and destination files, copies the source file contents to the destination file using a while loop, closes the files, and returns an error code or success message. Pseudocode and sample source code are provided to demonstrate the file copy algorithm.
The document describes a C program that copies one file to another using command line arguments. The program includes necessary headers, defines a main function that takes command line arguments, checks for valid arguments, opens the source and destination files, copies the source file contents to the destination file using a while loop, closes the files, and returns an error code or success message. Pseudocode and sample source code are provided to demonstrate the file copy algorithm.
AIM : WAP to copy one file to other (using command line arguments)
Software used: VS Code
Algorithm:
1. Include Necessary Header:
The code includes the necessary header file <stdio.h> for input/output operations. 2. Main Function: The main function takes command-line arguments ( int argc, char *argv[] ) to specify the source and destination file names. 3. Check Command-Line Arguments: Checks if the correct number of command-line arguments (source file and destination file) is provided. If not, it prints a usage message and returns an error code. 4. Open Source File for Reading: Uses fopen to open the source file ( argv[1]) in binary mode ( "rb"). Checks if the file opening is successful. If not, it prints an error message, closes any open files, and returns an error code. 5. Open Destination File for Writing: Uses fopen to open the destination file ( argv[2]) in binary mode ( "wb"). Checks if the file opening is successful. If not, it prints an error message, closes any open files (including the source file), and returns an error code. 6. Copy Contents of Source File to Destination File: Uses a while loop to read each character from the source file using fgetc until the end of the file ( EOF) is reached. Writes each character to the destination file using fputc. 7. Close Files: Uses fclose to close both the source and destination files. 8. Print Success Message: If the program reaches this point, it prints a success message indicating that the file copy was successful. 9. Return Codes: Returns 1, 2, or 3 in case of errors, indicating the type of error (incorrect command-line arguments, source file opening error, or destination file opening error). Returns 0 in case of successful execution. Flowchart:
Source Code: #include <stdio.h>
int main(int argc, char *argv[]) {
// Check if the correct number of command-line arguments is provided if (argc != 3) { printf("Usage: %s <source_file> <destination_file>\n", argv[0]); return 1; // Return error code }