Code:
#include<stdio.h>
#include<string.h>
main()
{
FILE *f1,*f2;
char str[10];
f1=fopen("file1.dat","w");
printf("Enter contents of file 1 :");
gets(str);
fputs(str,f1);
fclose(f1);
f1=fopen("file1.dat","r");
f2=fopen("file2.dat","w");
fputs(str,f2);
fclose(f1);
fclose(f2);
printf("Contents of copied file is :");
f2=fopen("file2.dat","r");
printf("%s \n",str);
fclose(f2);
}
I am new to this and hence when I was given this coding I tried understanding it. From what I can see, I am accepting a string str from the user and inserting it into File1. And then I am using the same variable "str" to insert content into file2. In this process I am not copying contents of file1 into file2. I am basically inserting the same string into file2. And that doesn't serve the purpose of the question. Am I right?
Comment