Trouble with a program on C

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 arrays TABA and TABB in main memory. The arrays 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.

That is what I wrote. First of all can you tell me is it correct? The problem is the merging of TABA and TABB in new arrat TABC. Can you helm me? :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<stdio.h>
#include <stdlib.h>

int *lire(char *nom_fichier,int *c);
void print(int *a,int n);


void main(void)
{    int n, *TABA, *TABB;
     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, copie dans le tableau dynamique TABA: \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, copie dans le tableau dynamique TABB: \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");
	}
It looks good so far. You need to have seperate variables for the size of TABA and TABB, as currently you only have one, n. You'll need to know the size of TABA and the size of TABB when you do the merge.
Topic archived. No new replies allowed.