1
I need to create a file that is a copy of the contents of two other files (already existing), the user will type 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 is in files 1 and 2 but when I check the third file (which should be the copy of file 1 + file 2), only one square symbol appears. The only explanation of how to make this code I found taught to read the entire file 1 and save in a certain variable, then by means of a fputs
insert the contents of the variable in the target file, in the video class I saw everything worked very well, and that’s what I did, but it didn’t work.
Follow the code:
#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;
}