Exercice IX_ 1 : Copier un fichier texte dans un autre: créer un fichier "essai
Exercice IX_ 1 : Copier un fichier texte dans un autre: créer un fichier "essai.dat" sous éditeur. Tester le programme en vérifiant après exécution la présence du fichier copié dans le directory. Exercice IX_2: Calculer et afficher le nombres de caractères d'un fichier ASCII (Utiliser n'importe quel fichier du répertoire). Exercice IX_3: Créer et relire un fichier binaire de 10 entiers. Exercice IX_4 : Lire un fichier texte, avec un contrôle 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 : Créer et relire un fichier texte de 5 chaînes de 3 caractères. Exercice IX_6: Ajouter une fiche (c'est à dire une chaîne de 3 caractères) au fichier précédent et relire le fichier. Exercice IX_7 : Rechercher une fiche dans le fichier précédent. Exercice IX_8 : Exercice récapitulatif: Créer une structure nom, prénom, âge. Ecrire un programme de gestion de fichier (texte) avec menu d'accueil: possibilité de créer le fichier, de le lire, d'y ajouter une fiche, d'en rechercher une 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(); } 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); 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(); } 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 "); 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 à créer : ") ; 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 (" numéro 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 demandée */ 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 début 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 après la position demandée */ 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; } 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 dans une liste vide */ int ins_dans_liste_vide (Liste * liste, char *donnee); /* insertion au début 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 uploads/Geographie/ exercice-fichier.pdf
Documents similaires










-
26
-
0
-
0
Licence et utilisation
Gratuit pour un usage personnel Attribution requise- Détails
- Publié le Aoû 20, 2021
- Catégorie Geography / Geogra...
- Langue French
- Taille du fichier 0.0658MB