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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int tri(FILE *flotLecture, FILE *flotEcriture,
int(*cmp)(const char *s1, const char *s2))
{
char ligne1[15];
char ligne2[15];
if(flotLecture == NULL)
return 1;
if(flotEcriture == NULL)
return 1;
while(fgets(ligne1, sizeof ligne1, flotLecture) != NULL)
while(fgets(ligne2, sizeof ligne2, flotLecture) != NULL)
{
char *tmp1;
if((*cmp)(ligne1,ligne2) > 0)
{
char buf[BUFSIZ];
strcpy(buf,ligne1);
tmp1 = malloc(strlen(ligne2)+1);
if(tmp1 == NULL)
return 1;
strcpy(tmp1,ligne2);
char *tmp2 = malloc(strlen(buf)+1);
if(tmp2 == NULL)
return 1;
strcpy(tmp2,buf);
}
fprintf(flotEcriture,"%s",tmp1);
}
return 0;
}
int main()
{
FILE *lecture = fopen("lire","r");
FILE *ecriture = fopen("ecrire","w");
if(tri(lecture,ecriture,&strcmp))
{
fprintf(stderr,"erreur \n");
}
return 0;
} |
Partager