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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc , char * argv[]) {
FILE * pfile_matrice;
FILE * pfile_nb_libs;
FILE * pfile_gsm;
FILE * pfile;
FILE * pfile_nb_tags;
FILE * pfile_libs;
FILE * pfile_tag;
int i ,freq, nb_libs, nb_tags, num_tag, num_lib, j;
int ** frequences;
char buffer_nom_lib[10];
char compare[10];
char * commande;
char buffer_sequence[18];
if (argc != 2 )
{
printf("il manque le nom du fichier à traiter!\n");
exit(1);
}
//allocation de mémoire et initialisation des variables
commande = (char *)malloc(100 * sizeof(char));
strcpy(compare , "");
num_lib = 0;
num_tag = 0;
j = 0;
nb_libs= 0;
//ouverture des fichiers
pfile = fopen(argv[1], "rb");
pfile_libs = fopen("noms_libs.txt","w+");
pfile_matrice = fopen("matrice.txt","w+");
fprintf(pfile_matrice, "Tag_sequence " ) ; //impression de l'entete
//récupération du nombre de tags et du nombre des librairies
pfile_nb_tags = fopen("nb_tags.txt", "rb");
fscanf(pfile_nb_tags, "%d", &nb_tags);
fclose(pfile_nb_tags);
pfile_nb_libs = fopen("nb_libs.txt", "rb");
fscanf(pfile_nb_libs, "%d", &nb_libs);
fclose(pfile_nb_libs);
//allocation de mémoire pour tableau d'entiers de dimension 2 qui va contenir la matrice
frequences = (int ** )malloc(nb_libs*sizeof(int *));
for (i =0; i <nb_libs; i++)
{
frequences[i] = (int *) malloc (nb_tags*sizeof(int));
}
//parcours du fichier de manière à reconnaitre les diffèrentes librairies
while(!feof(pfile))
{
fscanf(pfile, "%s", buffer_nom_lib);
fscanf(pfile, "%s " ,buffer_sequence);
fscanf(pfile, "%d" ,&freq);
//isolement de chaque librairies
if(strcmp(buffer_nom_lib, compare))
{
memset(commande,0,100);
num_lib++;
//recuperation des noms de lib et de leurs indices dans le fichiers noms_libs.txt
fprintf(pfile_libs, "_%d ----> %s\n", num_lib, buffer_nom_lib);
if( num_lib == nb_libs )
fprintf(pfile_matrice, "_%d\n", num_lib );
else fprintf(pfile_matrice, "_%d ", num_lib );
//recuperation d'un sous fichier contenant tous les tags se trouvant dans la librairies
strcpy(commande, "grep -w ");
strcat(commande, buffer_nom_lib);
strcat(commande, " fichier.txt > gsm.txt");
system(commande); //appel systeme de la commande grep : grep -w nom_lib fichier.txt > gsm.txt
//initialisation de la ligne du tableau correspondant a la librairie à 0, cela permet d'avoir une frequence nulle pour les tags qui ne se trouvent pas dans la librairie
for(i = 0 ; i< nb_tags; i ++)
{
frequences[num_lib-1][i]= 0;
}
//ouverture du sous fichier et remplissage de la ligne du tableau corrspondant à la librairies
pfile_gsm = fopen("gsm.txt", "rb");
while(!feof(pfile_gsm))
{
fscanf(pfile_gsm,"%s", buffer_nom_lib);
fscanf(pfile_gsm,"%d", &num_tag);
fscanf(pfile_gsm,"%d", &freq);
frequences[num_lib-1][num_tag-1] = freq;
}
fclose(pfile_gsm);
}
memset(compare, 0, 10);
strcpy(compare, buffer_nom_lib) ;
}
//affichage du nb de lib et de tags
printf("nb_libs : %d\n", nb_libs);
printf("nb_tags : %d\n", nb_tags);
// ecriture du tableau dans le fichier matrice.txt
pfile_tag = fopen("tag.txt", "rb");
for(i = 0; i < nb_tags; i ++)
{
fscanf(pfile_tag, "%s", buffer_sequence);
fprintf(pfile_matrice, "%s ", buffer_sequence);
for(j = 0; j < nb_libs-1; j++)
{
fprintf(pfile_matrice,"%d ", frequences[j][i] );
}
fprintf(pfile_matrice,"%d\n", frequences[nb_libs-1][i] );
}
//fermeture des fichiers et liberation de la mémoire
fclose(pfile_matrice);
fclose(pfile);
fclose(pfile_libs);
fclose(pfile_tag);
for (i= 0 ; i < nb_libs; i++)
{free(frequences[i]);}
free(frequences);
} |
Partager