How to copy the content of two arrays in third?
The goal of the exercise is:
Two files FA and FB whose names are entered on the keyboard contain integers sorted in ascending order. Write a program that copies the contents of FA and FB, respectively, in the dynamic tables TABA and TABB in main memory. Tables TABA and TABB are merged into a third sorted array in ascending TABC. After the merger, the TABC table is saved in a file whose name is FC to enter the keyboard. Memory for TABA, TABB TABC and whose numbers of elements are unknown, is reserved dynamically.
How to copy the content of TABA and TABB to a new array TABC?
Code:
#include<stdio.h>
#include <stdlib.h>
int *lire(char *nom_fichier,int *c);
void print(int *a,int n);
void main(void)
{ int n, i, *TABA, *TABB, *TABC;
char nom_fichier[10], nouveau_fichier[10];
printf("Entrer le nom du premier fichier: ") ;
scanf("%s", &nom_fichier);
TABA=lire(nom_fichier,&n);
printf("\nLe contenu du premier fichier:\n\n");
print(TABA,n);
printf("\nEntrer le nom du deuxiem fichier: ") ;
scanf("%s", &nom_fichier);
TABB=lire(nom_fichier,&n);
printf("\nLe contenu du deuxiem fichier:\n\n");
print(TABB,n);
}
int* lire(char *nom_fichier,int *c)
{ FILE *p;
int x, i,*tab, *t;
*c=0;
p=fopen(nom_fichier,"r");
if(p==NULL)
{ printf("Le fichier %s n'est pas ouvert!\n",nom_fichier); exit(1); }
fscanf(p,"%d",&x);
while(!feof(p))
{ (*c)++;
fscanf(p,"%d",&x);
}
tab=(int *)malloc(*c*sizeof(int));
if(tab==NULL)
{ printf("Allocation impossible!\n"); exit(1); }
t=tab;
rewind(p);
*c=0;
fscanf(p,"%d",tab);
while(!feof(p))
{ (*c)++;
fscanf(p,"%d",++tab);
}
fclose (p);
return t;
}
void print(int *a,int n)
{ int i;
for(i=0;i<n;i++,a++)
printf("%3d",*a);
printf("\n");
}