0% found this document useful (0 votes)
64 views4 pages

Copy File

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.

Uploaded by

nitinchandra834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

Copy File

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.

Uploaded by

nitinchandra834
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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
}

// Open the source file for reading


FILE *sourceFile = fopen(argv[1], "rb");
if (sourceFile == NULL) {
perror("Error opening source file");
return 2; // Return error code
}

// Open the destination file for writing


FILE *destinationFile = fopen(argv[2], "wb");
if (destinationFile == NULL) {
perror("Error opening destination file");
fclose(sourceFile);
return 3; // Return error code
}

// Copy the contents of the source file to the destination file


int ch;
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}

// Close the files


fclose(sourceFile);
fclose(destinationFile);

printf("File copy successful!\n");

return 0; // Return success code


}

Output:

You might also like