Description

上課時有進行檔案複製的練習。請撰寫一支程式,讀取 input.txt 的內容,並把讀到的內容複製到 output.txt 中。

During the class, there was an exercise on file copying. Please write a program that reads the contents of input.txt and copies the read content to output.txt.

Input (From File: input.txt)

包含多行文字,每行的文字不超過150個。

Contains multiple lines of text, each line containing no more than 150 characters.

Output (To File: output.txt)

請先在第一行印出 "This is a copy file:",空一行,接著把 input.txt 的內容都複製到 output.txt。

First, print "This is a copy file:" on the first line, followed by an empty line. Then copy all the contents of input.txt to output.txt.

Sample Input 1

Change is the essence of life,
a constant force shaping our existence.
From the shifting seasons to the
evolution of technology,
change surrounds us,
challenging us to adapt and grow. In the journey of life,
we encounter myriad transitions,
each presenting its own set of
opportunities and obstacles.

Sample Output 1

This is a copy file:

Change is the essence of life,
a constant force shaping our existence.
From the shifting seasons to the
evolution of technology,
change surrounds us,
challenging us to adapt and grow. In the journey of life,
we encounter myriad transitions,
each presenting its own set of
opportunities and obstacles.
#include <stdio.h> 
#include <stdlib.h> // For exit() 
#include <string.h>

int main() 
{ 
	FILE *fptr1, *fptr2; 
	char filename[100] = {"\\0"}, c; 
	char filename2[100] = {"\\0"};
//	printf("Enter the filename to open for reading \\n"); 
//	scanf("%s", filename); 
	strcat(filename,"input.txt");
//	printf("%s",filename);

	// Open one file for reading 
	fptr1 = fopen(filename, "r"); 
	if (fptr1 == NULL) 
	{ 
		printf("Cannot open file %s \\n", filename); 
		exit(0); 
	} 

//	printf("Enter the filename to open for writing \\n"); 
//	scanf("%s", filename); 
	strcat(filename2,"output.txt");
	
	
	// Open another file for writing 
	fptr2 = fopen(filename2, "w"); 
	if (fptr2 == NULL) 
	{ 
		printf("Cannot open file %s \\n", filename2); 
		exit(0); 
	} 

	// Read contents from file 
	
	c = fgetc(fptr1); 
	fprintf(fptr2,"This is a copy file:\\n");
		fprintf(fptr2,"\\n");

	while (c != EOF) 
	{ 
		fputc(c, fptr2); 
		c = fgetc(fptr1); 
	} 

//	printf("\\nContents copied to %s", filename); 

	fclose(fptr1); 
	fclose(fptr2); 
	return 0; 
}