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

Asked

Viewed 938 times

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

1 answer

1


So I’m a beginner in programming and I’m learning to code in C. A few 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 his problem was that the arqum and the arqdoiswere closed at the time you were passing the leitor and the leitor2to the arqtres, I mean, you were using the arqum and the arqdois to open the respective files requested by the user. This way, they served as pointers to the files that the user requested to read and as you closed them, it did not work the passage of the strings read arqtres. So open them up (do not know if this is the correct syntax of English 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 closed up after use. That’s it. I hope you understand.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.