Answer to Question #207420 in C for Zeeshan

Question #207420

Write a program to add the contents of one file at the end of another. 


1
Expert's answer
2021-06-17T07:15:34-0400
#include <stdio.h>
#include <stdlib.h> 


int main()
{
	FILE *firstFilePtr;
	FILE *secondFilePtr;
	char firstFileName[20];
	char secondFileName[20];
	char letter;
	printf("Enter the file name of the file for reading: ");
	gets(firstFileName);
	// Open one file for reading
	firstFilePtr = fopen(firstFileName, "r");
	if (firstFilePtr == NULL)
	{
		printf("%s file does not exist...", firstFileName);
		exit(0);
	}
	printf("Enter the file name to append the content : ");
	gets(secondFileName);
	// Open another file for appending content
	secondFilePtr = fopen(secondFileName, "a");
	if (secondFilePtr == NULL)
	{
		printf("%s file does not exist...", secondFileName);
		exit(0);
	}
	// Read the content from the file
	letter = fgetc(firstFilePtr);
	fputc('\n',secondFilePtr);
	while (letter != EOF)
	{
		fputc(letter,secondFilePtr);
		letter = fgetc(firstFilePtr);
	}
	fclose(firstFilePtr);
	fclose(secondFilePtr);


	printf("\nThe contents of the file \"%s\" has been added at the end of \"%s\".\n",firstFileName,secondFileName);
	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