Answer to Question #246307 in C for gryffin

Question #246307

Write a C program the `cp’ program that takes arguments as source and destination

filenames and copies the source to the destination. You need to use the functions fread

and fwrite. Note that the file may or may not be in text format.


1
Expert's answer
2021-10-04T03:07:05-0400
#include<stdio.h>
#include<string.h>




int main() {
	char letter;
	char sourceFileName[100];
	char destinationFileName[100];


	FILE *fpSourceFile, *fpDestinationFile;


	printf("Enter source file name: ");
	scanf("%s",&sourceFileName);
	if((fpSourceFile = fopen(sourceFileName, "rb"))!=NULL){
		printf("Enter destination file name: ");
		scanf("%s",&destinationFileName);
		fpDestinationFile = fopen(destinationFileName, "wb");


		while(fread(&letter, 1, 1, fpSourceFile) == 1){
			fwrite(&letter, 1, 1, fpDestinationFile);
		}
		printf("\nThe file has been copied.\n");
		// Close The Files
		fclose(fpSourceFile);
		fclose(fpDestinationFile);
	}else{
		printf("\nThe file does not exist.\n");
	}




	getchar();
	getchar();
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS