Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- fopen(<fichier>,<mode_ouverture>) : r (lecture seule, dois exister)
- w (écriture seule)
- a (ajout fin de fichier)
- r+ (lecture/ecriture, dois exister)
- w+ (lecture/ecriture, supprime contenu)
- a+ (ajout lecture/ecrite, fin de fichier)
- fclose(<fichier>) : fermer un fichier ouvert
- feof(<ficher>) : test la fin d'un fichier
- [LECTURE]
- fgetc(<fichier>) : lire un caractere
- fgets(<chaine>, <taille_chaine>, <fichier>) : lit une ligne
- fscanf(<fichier>, <format>,...) : lit du texte formaté
- [ECRITURE]
- fputc(<caratere>, <fichier>) : ecrit un caractere
- fputs(<chaine>, <fichier>) : écrit une ligne de texte
- fprintf(<fichier>,<format>,...) : ecrit du texte formaté
- */
- /*Ecrire un programme qui charge les mots du fichier MOTS.TXT
- dans la mémoire centrale, les trie d’après la méthode par
- propagation (méthode de la bulle) et les écrit dans un deuxième
- fichier MOTS_TRI.TXT.*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char const *argv[])
- {
- // Declaration variable
- FILE *pfichier = fopen("MOTS.TXT","w");
- char mot[50];
- char *tri;
- char *temp;
- int i,nb_mot=0,a,b;
- int longueur=0;
- // Initialisation interractive
- printf("Combien de mot voulez vous rentrer ? : ");
- scanf("%d",&nb_mot);
- // Ecriture dans le fichier
- for (i = 0; i < nb_mot; ++i)
- {
- printf("Mot #%d : ",i+1);
- scanf("%50s",&mot);
- longueur=longueur+strlen(mot);
- fprintf(pfichier,"%s\n",mot );
- }
- tri = malloc(sizeof(char)*longueur);
- fclose(pfichier);
- // Lecture
- pfichier = fopen("MOTS.TXT","r");
- if (pfichier==NULL)
- {
- puts("erreur");
- exit(1);
- }
- i=0;
- while (fgets(mot,50,pfichier))
- {
- printf("%s",mot);
- tri=mot;
- i++;
- }
- fclose(pfichier);
- // Tri
- for (a = 0; a < nb_mot-1; a++)
- {
- for (b = a+1; b < nb_mot; b++)
- {
- if (*(tri+a)>*(tri+b))
- {
- temp = *(tri+a);
- *(tri+a) = *(tri+b);
- *(tri+b)= temp;
- }
- }
- }
- // Affichage
- for (i = 0; i < nb_mot; ++i)
- {
- printf("%s\n",tri[i] );
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment