I need to create a file that is the copy of the contents of two other files (already exist), the user will enter the name of these two files.
In my case, the first file contains: "hi, banana, cheese", and the second: "jelly, ice cream". Apparently the program runs very well, and it can read what it has in files 1 and 2 but when I go check the third file (which should be the copy of file 1 + file 2), only a square symbol appears. The only explanation of how to do this code that I thought taught to read the whole file 1 and save in a certain variable, then by means of a fputs
insert the contents of the variable in the recipient file, in the video lesson that I saw everything worked very well , and that's what I did, but it did not work out.
Follow the code below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *arqum;
FILE *arqdois;
FILE *arqtres;
char ch,ch1,leitor[100],leitor2[100];
char nome1[20],nome2[20];
printf("\nDigite o nome primeiro arquivo:\n");
gets(nome1);
arqum=fopen(nome1, "r"); //abre o arquivo para leitura
if(NULL==arqum)
{
printf("O arquivo não pode ser aberto. \n" );
system("Pause");
exit (1);
}
ch=fgetc(arqum);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(arqum);
}
fclose (arqum);
printf("\nDigite o nome do segundo arquivo:\n");
gets(nome2);
arqdois=fopen(nome2, "r"); //abre o arquivo para leitura
if(NULL==arqum)
{
printf("O arquivo não pode ser aberto. \n" );
system("Pause");
exit (1);
}
ch1=fgetc(arqum);
while(ch1!=EOF)
{
putchar(ch1);
ch1=fgetc(arqdois);
}
fclose (arqdois);
arqtres=fopen("arquivotres.txt","a+");
if(NULL==arqtres)
{
printf("O arquivo não pode ser aberto. \n" );
system("Pause");
exit (1);
}
while(fgets(leitor,100,arqum)!=NULL);
fputs(leitor,arqtres);
fclose(arqtres);
arqtres=fopen("arquivotres.txt","a+");
while(fgets(leitor2,100,arqdois)!=NULL);
fputs(leitor2,arqtres);
fclose(arqtres);
return 0;
}