Write a small program that copies everything in a text file to another file. The
ID: 3774311 • Letter: W
Question
Write a small program that copies everything in a text file to another file. The text file is called "in.txt" for reading and the output file is called "out.txt". Two files should be exactly the same after copying (the first file shouldn't be changed, and the second file should be the same as the first one). Your program should define a function to copy the input file to the output file with two arguments: a file pointer to the input file and a file pointer to the output file. In Program 4, you will need use this function again. When submit this program, please just use the file names without any path information. Otherwise, the grader can't check the results.Explanation / Answer
Answer
#include <stdio.h>
#include <conio.h>
void fun(FILE* file1,FILE* file2)
{
char line[1000];
while (fgets(line,1000, file1)!=NULL)
{
fprintf(file2,"%s",line);
}
}
void main()
{
FILE* file1 = fopen("in.txt","r");
FILE* file2 = fopen("out.txt","w");
clrscr();
if (file1 == NULL && file2 == NULL)
{
printf("Error! opening file");
}
else
{
fun(file1,file2);
}
printf("Data written into file.");
fclose(file1);
fclose(file2);
getch();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.