un deux trois quatre cinq
deux trois quatre cinq
#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; long courant; while(fgets(ligne1, sizeof ligne1, flotLecture) != NULL) { courant = ftell(flotLecture); while(fgets(ligne2, sizeof ligne2, flotLecture) != NULL) { if((*cmp)(ligne1,ligne2) > 0) { char buf[BUFSIZ]; strcpy(buf,ligne1); char *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); free(tmp1); free(tmp2); } } fseek(flotLecture,courant,SEEK_CUR); } fclose(flotEcriture); fclose(flotLecture); return 0; } int main(int argc, char *argv[]) { FILE *lecture = fopen("lire","r"); FILE *ecriture = fopen("ecrire","w"); if(tri(lecture,ecriture,&strcmp)) { fprintf(stderr,"erreur \n"); } return EXIT_SUCCESS; }
-