0% ont trouvé ce document utile (0 vote)
433 vues7 pages

Gestion de fichiers en C : Exemples pratiques

Transféré par

lopir120
Copyright
© Attribution Non-Commercial (BY-NC)
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOC, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
433 vues7 pages

Gestion de fichiers en C : Exemples pratiques

Transféré par

lopir120
Copyright
© Attribution Non-Commercial (BY-NC)
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats DOC, PDF, TXT ou lisez en ligne sur Scribd

Exercice IX_1: Copier un fichier texte dans un autre: crer un fichier "essai.dat" sous diteur.

Tester le programme en vrifiant aprs excution la prsence du fichier copi dans le directory. Exercice IX_2: Calculer et afficher le nombres de caractres d'un fichier ASCII (Utiliser n'importe quel fichier du rpertoire). Exercice IX_3: Crer et relire un fichier binaire de 10 entiers. Exercice IX_4: Lire un fichier texte, avec un contrle d'erreur: L'utilisateur saisit le nom du fichier, la machine retourne le listing du fichier s'il existe et un message d'erreur s'il n'existe pas. Exercice IX_5: Crer et relire un fichier texte de 5 chanes de 3 caractres. Exercice IX_6: Ajouter une fiche (c'est dire une chane de 3 caractres) au fichier prcdent et relire le fichier. Exercice IX_7: Rechercher une fiche dans le fichier prcdent. Exercice IX_8: Exercice rcapitulatif: Crer une structure nom, prnom, ge. Ecrire un programme de gestion de fichier (texte) avec menu d'accueil: possibilit de crer le fichier, de le lire, d'y ajouter une fiche, d'en rechercher Exercice IX_ 1: #include <stdio.h> #include <conio.h> void main() { FILE *fichier1,*fichier2; char c; printf("COPIE EN COURS ...\n"); fichier1 fopen("c:\\bc5\\Courc_C\\Teach_C\\CHAP9\\essai.dat","r"); fichier2 = fopen("copie.dat","w"); while((c=(char)getc(fichier1))!=EOF)putc(c,fichier2); fclose(fichier1); fclose(fichier2); printf("C'EST FINI !\n"); printf("\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); }

printf("TAILLE DU FICHIER: %d OCTETS\n",compteur); printf("\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); } Exercice IX_3: #include <stdio.h> #include <conio.h> void main() { FILE *fichier; int i,n; fichier = fopen("nombre.dat","wb+"); for(i=0;i<10;i++) { printf("N = "); scanf("%d",&n); putw(n,fichier); } rewind(fichier); while(!feof(fichier)) /* essayer avec une boucle for */ { n=getw(fichier); printf("%d ",n); } fclose(fichier); printf("\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); }

une

Exercice IX_4:
#include <stdio.h> #include <conio.h> void main() { FILE *fichier; char c,nom[10]; printf("NOM DU FICHIER A LISTER: "); gets(nom); if((fichier = fopen(nom,"r"))==NULL) printf("\nERREUR A L'OUVERTURE, CE FICHIER N'EXISTE PAS\n"); else { printf("\n\t\t\tLISTING DU FICHIER\n"); printf("\t\t\t------------------\n\n"); while((c=getc(fichier))!=EOF)printf("%c",c); } fclose(fichier); printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE ");

Exercice IX_2: #include <stdio.h> #include <conio.h> void main() { FILE *fichier; int compteur=0; fichier = fopen("c:\\bc5\\Courc_C\\teach_C\\chap9\\copie.dat","r"); while(getc(fichier)!=EOF)compteur++; fclose(fichier);

getch(); } Exercice IX_5: #include <stdio.h> #include <conio.h> void main() { FILE *fichier; int i; char p[4]; /* saisie du fichier */ fichier = fopen("chaine.dat","w+"); printf("\nENTRER 5 CHAINES DE 3 CARACTERES\n"); for(i=0;i<5;i++) { gets(p); fputs(p,fichier); } rewind(fichier); /* pointeur au debut */ /* relecture du fichier */ printf("\n\nLECTURE DU FICHIER\n\n"); while((fgets(p,4,fichier))!=NULL)printf("%s ",p); fclose(fichier); printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); } Exercice IX_6: #include <stdio.h> #include <conio.h> void main() { FILE *fichier; char p[4]; /* ajout d'une chaine */ fichier = fopen("chaine.dat","a+"); /* pointeur a la fin */ printf("\nENTRER LA CHAINE DE 3 CARACTERES A AJOUTER\n"); gets(p); fputs(p,fichier); /*lecture du fichier pour verification */ rewind(fichier); printf("\n\nLECTURE DU FICHIER\n"); while((fgets(p,4,fichier))!=NULL)printf("%s ",p); fclose(fichier);

printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); } Exercice IX_7: #include <stdio.h> #include <conio.h> #include <string.h> void main() { FILE *fichier; char p[4],q[4],trouve=0; /* recherche d'une chaine */ fichier = fopen("chaine.dat","r"); /* lecture seule */ printf("\nENTRER LA CHAINE DE 3 CARACTERES RECHERCHEE\n"); gets(p); printf("\n\nRECHERCHE ...\n\n"); while(((fgets(q,4,fichier))!=NULL)&&(trouve==0)) if(strcmp(p,q)==0)trouve = 1; /* compare les chaines */ if (trouve ==0) printf("CETTE CHAINE N'EXISTE PAS DANS LE FICHIER\n"); else printf("CHAINE TROUVEE DANS LE FICHIER\n"); /*lecture du fichier pour verification */ rewind(fichier); printf("\n\nLECTURE DU FICHIER\n"); while((fgets(q,4,fichier))!=NULL)printf("%s ",q); fclose(fichier); printf("\n\nPOUR SORTIR FRAPPER UNE TOUCHE "); getch(); } Exercice IX_8: #include <stdio.h> #include <conio.h> #include <string.h> typedef struct { char nom[10]; char prenom[10]; int age; } carte; /* creation d'un type carte */ void creer_fichier(FILE *f,char *n) { char choix; carte fiche;

clrscr(); printf("CREATION DU FICHIER \n\n"); printf("NOM DU FICHIER A CREER: "); gets(n); flushall(); f = fopen(n,"w"); do { printf("\nSAISIE D'UNE FICHE ?(o/n) "); choix = (char)getchar(); flushall(); if ((choix=='o')||(choix=='O')) { printf("\nNOM: ");gets(fiche.nom); printf("PRENOM: ");gets(fiche.prenom); printf("AGE: ");scanf("%d",&fiche.age); flushall(); fwrite(&fiche,sizeof(carte),1,f); } } while((choix=='o')||(choix=='O')); fclose(f); } void lire_fichier(FILE *f,char *n) { carte fiche; int compteur=0; clrscr(); printf("LECTURE DU FICHIER\n\n"); printf("NOM DU FICHIER A LIRE: ");gets(n); flushall(); f = fopen(n,"r"); if (f == NULL) printf("\nERREUR, CE FICHIER N'EXISTE PAS\n\n"); else { printf("\nLISTING DU FICHIER\n\n"); while(fread(&fiche,sizeof(carte),1,f)!=0) { printf("fiche n%d: \n",compteur); compteur++; printf("%s %s %d an(s)\n\n",fiche.nom,fiche.prenom,fiche.age); } fclose(f); } printf("POUR CONTINUER FRAPPER UNE TOUCHE "); getch(); } void ajout(FILE *f,char *n) { carte fiche; char choix;

clrscr(); printf("AJOUT D'UNE FICHE \n\n"); printf("NOM DU FICHIER A MODIFIER: "); gets(n); flushall(); f = fopen(n,"a"); do { printf("\nSAISIE D'UNE FICHE ?(o/n) "); choix = (char)getchar(); flushall(); if ((choix=='o')||(choix=='O')) { printf("\nNOM: ");gets(fiche.nom); printf("PRENOM: ");gets(fiche.prenom); printf("AGE: ");scanf("%d",&fiche.age); flushall(); fwrite(&fiche,sizeof(carte),1,f); } } while((choix=='o')||(choix=='O')); fclose(f); } void recherche(FILE *f,char *n) { carte fiche; int compteur=0; char trouve = 0,nn[10],pp[10]; clrscr(); printf("RECHERCHE DE FICHE\n\n"); printf("NOM DU FICHIER: "); gets(n); flushall(); f = fopen(n,"r"); printf("\nFICHE A RETROUVER:\n"); printf("NOM: ");gets(nn); printf("PRENOM: ");gets(pp); flushall(); while((fread(&fiche,sizeof(carte),1,f)!=0)&&(trouve==0)) { if((strcmp(fiche.nom,nn)==0)&&(strcmp(fiche.prenom,p p)==0)) { trouve=1; printf("FICHE RETROUVEE: FICHE n %2d\n",compteur); } compteur++; } if (trouve==0)printf("CETTE FICHE N'EXISTE PAS\n"); fclose(f); printf("POUR CONTINUER FRAPPER UNE TOUCHE "); getch(); }

void main() { FILE *fichier; char nom[10]; /* nom du fichier */ char choix; do { clrscr(); printf("\t\t\tGESTION DE FICHIER\n"); printf("\t\t\t------------------\n\n\n"); printf("CREATION DU FICHIER ---> 1\n"); printf("LECTURE DU FICHIER ---> 2\n"); printf("AJOUTER UNE FICHE ---> 3\n"); printf("RECHERCHER UNE FICHE ---> 4\n"); printf("SORTIE ---> S\n\n"); printf("VOTRE CHOIX: "); choix = (char)getchar(); flushall(); switch(choix) { case '1':creer_fichier(fichier,nom);break; case '2':lire_fichier(fichier,nom);break; case '3':ajout(fichier,nom);break; case '4':recherche(fichier,nom);break; } } while ((choix!='S') && (choix!='s')); } 1 { char nomfich[21] ; int n ; FILE * sortie ; printf ("nom du fichier crer : ") ; scanf ("%20s", nomfich) ; sortie = fopen (nomfich, "w") ; do {printf ("donnez un entier : ") ; scanf ("%d", &n) ; if (n) fwrite(&n, sizeof(int), 1, sortie) ; } while (n) ; fclose (sortie) ; retunr 0 ; } 2{ char nomfich[21] ; int n ; FILE * entree ; printf ("nom du fichier lister : ") ; scanf ("%20s", nomfich) ; entree = fopen (nomfich, "r") ; while ( fread (&n, sizeof(int), 1, entree), ! feof(entree) ) { printf ("\n%d", n) ; } fclose (entree) ; return 0 ; } 3{ char nomfich[21]; int n ; long num ; FILE * entree ; printf ("nom du fichier consulter : ") ;

scanf ("%20s", nomfich) ; entree = fopen (nomfich, "r") ; while ( printf (" numro de l'entier recherch : "),scanf ("%ld", &num), num ) {fseek (entree, sizeof(int)*(num-1), SEEK_SET) fread (&n, sizeof(int), 1, entree) ; printf (" valeur : %d \n", n) ; } fclose (entree) ; return 0 ; } 4{ char nomfich[21] ; FILE * sortie ; long num ; int n ; printf ("nom fichier : ") ; scanf ("%20s",nomfich) ; sortie = fopen (nomfich, "w") ; while (printf("\nrang de l'entier : "), scanf("%ld",&num), num) {printf ("valeur de l'entier : ") ; scanf ("%d", &n) ; fseek (sortie, sizeof(int)*(num-1), SEEK_SET); fwrite (&n, sizeof(int), 1, sortie) ; } fclose(sortie) ; return 0 ; } /*insertion la fin de la liste */ int ins_fin_liste (Liste * liste, Element * courant, char *donnee){ Element *nouveau_element; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1; if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; strcpy (nouveau_element->donnee, donnee); courant->suivant = nouveau_element; nouveau_element->suivant = NULL; liste->fin = nouveau_element; liste->taille++; return 0;

} /* insertion la position demande */ int ins_liste (Liste * liste, char *donnee, int pos){ if (liste->taille < 2) return -1; if (pos < 1 || pos >= liste->taille) return -1; Element *courant; Element *nouveau_element; int i; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1; if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; courant = liste->debut;

for (i = 1; i < pos; ++i) courant = courant->suivant; if (courant->suivant == NULL) return -1; strcpy (nouveau_element->donnee, donnee); nouveau_element->suivant = courant>suivant; courant->suivant = nouveau_element; liste->taille++; return 0; } /* suppression au dbut de la liste */ int supp_debut (Liste * liste){ if (liste->taille == 0) return -1; Element *supp_element; supp_element = liste->debut; liste->debut = liste->debut->suivant; if (liste->taille == 1) liste->fin = NULL; free (supp_element->donnee); free (supp_element); liste->taille--; return 0; } /* supprimer un element aprs la position demande */ int supp_dans_liste (Liste * liste, int pos){ if (liste->taille <= 1 || pos < 1 || pos >= liste->taille) return -1; int i; Element *courant; Element *supp_element; courant = liste->debut; for (i = 1; i < pos; ++i) courant = courant->suivant; supp_element = courant->suivant; courant->suivant = courant->suivant>suivant; if(courant->suivant == NULL) liste->fin = courant; free (supp_element->donnee); free (supp_element); liste->taille--; return 0; }

/* insertion dans une liste vide */ int ins_dans_liste_vide (Liste * liste, char *donnee); /* insertion au dbut de la liste */ int ins_debut_liste (Liste * liste, char *donnee); /* insertion a fin de la liste */ int ins_fin_liste (Liste * liste, Element * courant, char *donnee); /* insertition ailleurs */ int ins_liste (Liste * liste, char *donnee, int pos); /* SUPPRESSION */ int supp_debut (Liste * liste); int supp_dans_liste (Liste * liste, int pos); int menu (Liste *liste,int *k); void affiche (Liste * liste); void detruire (Liste * liste); /* -------- FIN liste.h --------- */

liste_function.h
/***************************\ * liste_function.h * \***************************/ void initialisation (Liste * liste) { liste->debut = NULL; liste->fin = NULL; liste->taille = 0; } /* insertion dans une liste vide */ int ins_dans_liste_vide (Liste * liste, char *donnee){ Element *nouveau_element; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1; if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; strcpy (nouveau_element->donnee, donnee); nouveau_element->suivant = NULL; liste->debut = nouveau_element; liste->fin = nouveau_element; liste->taille++; return 0;

Exemple complet
/* ---------- liste.h ----------- */ typedef struct ElementListe { char *donnee; struct ElementListe *suivant; } Element; typedef struct ListeRepere { Element *debut; Element *fin; int taille; } Liste; /* initialisation de la liste */ void initialisation (Liste * liste); /* INSERTION */

/* insertion au dbut de la liste */ int ins_debut_liste (Liste * liste, char *donnee){ Element *nouveau_element; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1;

if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; strcpy (nouveau_element->donnee, donnee); nouveau_element->suivant = liste->debut; liste->debut = nouveau_element; liste->taille++; return 0; } /*insertion la fin de la liste */ int ins_fin_liste (Liste * liste, Element * courant, char *donnee){ Element *nouveau_element; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1; if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; strcpy (nouveau_element->donnee, donnee); courant->suivant = nouveau_element; nouveau_element->suivant = NULL; liste->fin = nouveau_element; liste->taille++; return 0; } /* insertion la position demande */ int ins_liste (Liste * liste, char *donnee, int pos){ if (liste->taille < 2) return -1; if (pos < 1 || pos >= liste->taille) return -1; Element *courant; Element *nouveau_element; int i; if ((nouveau_element = (Element *) malloc (sizeof (Element))) == NULL) return -1; if ((nouveau_element->donnee = (char *) malloc (50 * sizeof (char))) == NULL) return -1; courant = liste->debut; for (i = 1; i < pos; ++i) courant = courant->suivant; if (courant->suivant == NULL) return -1; strcpy (nouveau_element->donnee, donnee); nouveau_element->suivant = courant>suivant; courant->suivant = nouveau_element; liste->taille++; return 0; } /* suppression au dbut de la liste */ int supp_debut (Liste * liste){ if (liste->taille == 0) }

return -1; Element *supp_element; supp_element = liste->debut; liste->debut = liste->debut->suivant; if (liste->taille == 1) liste->fin = NULL; free (supp_element->donnee); free (supp_element); liste->taille--; return 0; /* supprimer un element aprs la position demande */ int supp_dans_liste (Liste * liste, int pos){ if (liste->taille <= 1 || pos < 1 || pos >= liste->taille) return -1; int i; Element *courant; Element *supp_element; courant = liste->debut; for (i = 1; i < pos; ++i) courant = courant->suivant; supp_element = courant->suivant; courant->suivant = courant->suivant>suivant; if(courant->suivant == NULL) liste->fin = courant; free (supp_element->donnee); free (supp_element); liste->taille--; return 0; } /* affichage de la liste */ void affiche (Liste * liste){ Element *courant; courant = liste->debut; while (courant != NULL){ printf ("%p - %s\n", courant, courant->donnee); courant = courant->suivant; } } /* detruire la liste */ void detruire (Liste * liste){ while (liste->taille > 0) supp_debut (liste); } int menu (Liste *liste,int *k){ int choix; printf("********** MENU **********\n"); if (liste->taille == 0){ printf ("1. Ajout du 1er element\n"); printf ("2. Quitter\n"); }else if(liste->taille == 1 || *k == 1){ printf ("1. Ajout au debut de la liste\n"); printf ("2. Ajout a la fin de la liste\n"); printf ("4. Suppression au debut de la liste\n"); printf ("6. Detruire la liste\n"); printf ("7. Quitter\n"); }else {

printf ("1. Ajout au debut de la liste\n"); printf ("2. Ajout a la fin de la liste\n"); printf ("3. Ajout apres la position specifie\n"); printf ("4. Suppression au debut de la liste\n"); printf ("5. Suppression apres la position specifie\n"); printf ("6. Detruire la liste\n"); printf ("7. Quitter\n"); } printf ("\n\nFaites votre choix : "); scanf ("%d", &choix); getchar(); if(liste->taille == 0 && choix == 2) choix = 7; return choix; } /* -------- FIN liste_function.h --------*/

Vous aimerez peut-être aussi