How to copy the contents of two files into a third file in c?

1

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;
}
    
asked by anonymous 11.06.2017 / 05:15

1 answer

1

So, I'm a beginner in programming and I'm learning to program in C. The days ago I learned about files (in fact I'm still learning). Your code working correctly is this:

#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);
  }

  arqum=fopen(nome1, "r"); //abre o arquivo para leitura DE NOVO
  if(NULL==arqum)
  {
      printf("O arquivo não pode ser aberto. \n" );
      system("Pause");
      exit (1);
  }

  while(fgets(leitor,100,arqum)!=NULL);
  fputs(leitor,arqtres);
  fclose(arqtres);
  fclose (arqum); // fecha arquivo um

  arqdois=fopen(nome2, "r"); //abre o arquivo para leitura DE NOVO
  if(NULL==arqudois)
  {
      printf("O arquivo não pode ser aberto. \n" );
      system("Pause");
      exit (1);
  }

  arqtres=fopen("arquivotres.txt","a+");
  while(fgets(leitor2,100,arqdois)!=NULL);
  fputs(leitor2,arqtres);

  fclose (arqdois); // fecha arquivo dois
  fclose(arqtres);
return 0;
}

The solution to your problem was that arqum and arqdois were closed at the time you were passing leitor and leitor2 to arqtres , ie you were using arqum and arqdois to open the respective files requested by the user. In this way, they served as pointers to the files the user requested to read and how you closed them, it did not make sense to pass the strings read to arqtres . Then open them (I do not know if this is the correct syntax of Portuguese kkk) again. You can see in the lines

arqum=fopen(nome1, "r"); //abre o arquivo para leitura DE NOVO

and

arqdois=fopen(nome2, "r"); //abre o arquivo para leitura DE NOVO

and then I was closing after use. It is. I hope you have understood.

    
11.06.2017 / 16:36