Merci Sébastien pour ton aide.
Si tu veux, à la base c'est un programme de comparaison de 2 fichiers,
en le testant j'ai l'impression qu'il me sort des résultat aléatoire.
voila les fichiers en entrée
fich1 :
II-LP;35554 ;SGN ;EN;2
II-LP;35554 ;SGN ;EN;6
II-LP;35574 ;SGN ;EN;4
II-LP;35574 ;SGN ;EN;6
fich2:
II-LP;35554 ;SGN ;EN;1
II-LP;35559 ;SGN ;EN;7
II-LP;35574 ;SGN ;EN;5
II-LP;35574 ;SGN ;EN;0
fichier rélultat par le programme ""liste.tmp""
II-LP;35554 ;SGN ;EN;1
II-LP;35559 ;SGN ;EN;7
II-LP;35559 ;SGN ;EN;7
II-LP;35559 ;SGN ;EN;7
II-LP;35574 ;SGN ;EN;5
II-LP;35574 ;SGN ;EN;5
II-LP;35574 ;SGN ;EN;0
II-LP;35574 ;SGN ;EN;0
Alors que je m'attandais à un fichier ce de type :
II-LP;35554 ;SGN ;EN;1
II-LP;35559 ;SGN ;EN;7
II-LP;35574 ;SGN ;EN;5
II-LP;35574 ;SGN ;EN;0
en gros une ligne modifiée dans le fich2 , elle doit etre reporter dans le fichier résultat.
une ligne modifiée dans le fich2 , il faut la reporté avec toutes les lignes (si y a plusieurs lignes de meme identifiant )(que la ligne modifé) dans fichier resultat.
et danc je crois tte se passe dans la fonction recopie.!!!!
j'ai du mal sincérement
la je mets à ta disposition tt le code du programme. si t'es peux m'aider .
N.B pour complier ss unix ==== > make main ( le nom du programme est main.c)
pour exécuter ====> main fich1 fich2
je mets les le main.c les 2 fichiers ds le meme répertoire pour faciliter les testes.
le code
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Parametres ajustables */
/* #define LIGNES_PAR_PAGE 60 */
/* declaration des fonctions */
int main(int, char **);
void verif(int, char **);
void recopie(char *, char *, char *);
void erreur(int, char *);
/* variables globales */
FILE *fin1, *fin2, *fout;
char fichout[100];
char fichin1 [100];
char fichin2 [100];
int app;
/* definition des fonctions */
int main(int argc, char * argv[])
{
verif(argc,argv);
if ((fin1=fopen(fichin1,"r")) == NULL) erreur(3,fichin1);
if ((fin2=fopen(fichin2,"r")) == NULL) erreur(3,fichin2);
if ((fout=fopen(fichout,"w")) == NULL)
{fclose(fin1);fclose(fin2);
erreur(4,fichout);
}
recopie(fichin1, fichin2, fichout);
if (fclose(fout) != 0) erreur(5, fichout);
if (fclose(fin1) != 0) erreur(5, fichin1);
if (fclose(fin2) != 0) erreur(5, fichin2);
/* printf("\n\n %5d lignes ont ete copiees", nlignetot);*/
}
void verif(int argc, char **argv)
/* verification des parametres */
{
int i,j,k,l,min,max;
char arg[100];
char c1,c2,c3;
char lin[255];
/* valeurs par defaut */
strcpy(fichin1,"$$$$$$$");
strcpy(fichin2,"$$$$$$$");
strcpy(fichout,"$$$$$$$");
/*
printf("les fichiers sont : %s, %s\n", fichin, fichout);
*/
for (i=1; i<argc; i++)
{strcpy(arg, argv[i]);
if (arg[0] == '-' )
switch (arg[1]) {
case '?': erreur(6," ");
break;
default : erreur(6,arg);
}
else {
if ( strcmp(fichin1,"$$$$$$$") == 0 )
strcpy(fichin1,arg);
else {
if ( strcmp(fichin2,"$$$$$$$") == 0 )
strcpy(fichin2,arg);
else
{if ( strcmp(fichout,"$$$$$$$") == 0 )
strcpy(fichout,arg);
else
erreur(6, "trop de nom de fichier");
}
}
}
}
if ( strcmp(fichout,"$$$$$$$") == 0 )
strcpy(fichout, "liste.tmp");
/*
printf("les fichiers sont : %s, %s\n", fichin, fichout);
*/
}
void recopie(char *source1, char *source2, char *cible)
{
char lin1[2255], lin2[2255], lin1a[100], lin2a[100], lin2as[100], lin2as_old[100];
int page, ligne, i, j, k, l ;
int cmp, multi, nbmulti;
int ficOrg, ficOrg2;
char tlp[10][300];
page=0; ligne=0; nbmulti=multi=0;
strcpy(lin2as_old,"xxx");
fgets(lin1,2255,fin1);
fgets(lin2,2255,fin2);
strncpy(lin1a,&lin1[2],6);
lin1a[5]=0;
if(strcmp(lin1a, "EN-OS") == 0) ficOrg=1; else ficOrg=0;
strncpy(lin2a,&lin2[2],6);
lin2a[5]=0;
if(strcmp(lin2a, "EN-OS") == 0) ficOrg2=1; else ficOrg2=0;
if (ficOrg != ficOrg2) erreur(8, "fichiers incoherents");
while (feof(fin1)==0 || feof(fin2)==0)
{
if(ficOrg) {
strncpy(lin1a,&lin1[8],4);
lin1a[4]=0;
strncpy(lin2a,&lin2[8],4);
lin2a[4]=0;
} else {
strncpy(lin1a,&lin1[0],5);
lin1a[5]=0;
strncpy(lin2a,&lin2[0],5);
lin2a[5]=0;
if (strcmp(lin2a, "II-LP") == 0 || strcmp(lin2a, "II-ED") == 0 ){
i=j=0;
for(k=7; k<24; k++)if(lin2[k]==';'&& i==0)i=k;
strncpy(lin2as,lin2,i);
lin2as[i] = 0;
if (strcmp(lin2as, lin2as_old) == 0) {
nbmulti = nbmulti+1;
if(nbmulti > 10) erreur(6,"10");
} else {
if (multi==1) {
/* printf("Ecriture %s \n", lin2as_old); */
for(i=0;i<=nbmulti;i++) fputs(&tlp[i][0],fout);
multi=0;
}
nbmulti = 0;
strcpy(lin2as_old, lin2as);
}
strcpy(&tlp[nbmulti][0], lin2);
}
}
if (feof(fin1)==0 && feof(fin2)==0)
{cmp= strcmp(&lin1[2], &lin2[2]);
if (cmp != 0 && ficOrg) {
if(strcmp(lin1a, lin2a) == 0) { /* mise à jour */
fgets(lin1,2255,fin1);
cmp=1;
}
}
if (cmp != 0 && !ficOrg) { /* si ligne modif : OK */
if (strcmp(lin1a, lin2a) != 0) {
/* ordre = II-EM < II-PI > II-CJ < II-ST > II-LP > II-ED */
if(strcmp(lin1a, "II-PI") == 0 && strcmp(lin2a, "II-CJ") == 0) cmp= -1;
if(strcmp(lin1a, "II-ST") == 0 && strcmp(lin2a, "II-LP") == 0) cmp= -1;
if(strcmp(lin1a, "II-LP") == 0 && strcmp(lin2a, "II-ED") == 0) cmp= -1;
}
}
}
if (feof(fin1)==0 && feof(fin2)!=0)
cmp= -1;
if (feof(fin1)!=0 && feof(fin2)==0)
cmp= +1;
if (cmp ==0) {
fgets(lin1,2255,fin1);
fgets(lin2,2255,fin2);
}
if (cmp < 0) {
if (ficOrg) {
/* si code unité de longueur variable, remplacer 14 par la position après le 3e ; */
lin1[14] = '0';
fputs(lin1,fout);
}
fgets(lin1,2255,fin1);
}
if (cmp > 0) {
if (strcmp(lin2a, "II-LP") == 0 || strcmp(lin2a, "II-ED") == 0 ){
multi=1;
} else {
fputs(lin2,fout);
}
fgets(lin2,2255,fin2);
}
}
if (multi==1) {
/*printf("Ecriture %s \n", lin2as_old); */
for(i=0;i<=nbmulti;i++) fputs(&tlp[i][0],fout);
}
/*
printf("\n %5d lignes copiees de %s sur %s,ligne,source,cible);
nlignetot=nlignetot+ligne;
*/
}
void erreur(int no, char *str)
{
char *msg[] = {"nombre de parametres incorrect",
"erreur de syntaxe",
"fichier impossible a ouvrir",
"fichier impossible a ouvrir ou a creer",
"erreur a la fermeture du fichier",
"les parametres connus sont: fic_in1 fic_in2 fic_out",
"erreur conversion numerique",
"fichier historique impossible a ouvrir",
"nombre d'historique Langue/Education trop petit"};
printf("\n *** %s : %s\n",msg[no-1],str);
exit(1);
}