Le traitement des fichiers

Résolu
limbergh Messages postés 669 Statut Membre -  
TSniper Messages postés 796 Statut Membre -
Bonsoir,

je vois le traitement des fichiers à mes cours du soir et je ne
comprend pas tout d'un code...
le fonctionnement :

on demande à l'utilisateur d'introduire le nom, prénom, age et taille
de plusieurs personnes et de les enregistrer dans un fichier.
Tout fonctionne mais je ne comprend pas tout, c'est un code que j'ai fait
mais en le calquant sur l'exemple du cours... :
D'où je n'ai pas tout compris

Le voici

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define taillemax 30
 
typedef struct personne  
{
char nom[taillemax];
char prenom[taillemax];
int age;
int taille;
}pers;
 
void ftrinom(pers *t, int nbis);
void ftriage(pers *t, int nbis);
 
int main(int argc, char *argv[])
{
 
pers p, tableau[4];
char nom2[taillemax];
char prenom2[taillemax];
int i=0, nbpers;
FILE *fichier;
char nomfichier[taillemax];
 
do
{
printf("\n nom : ");
gets(nom2);
printf("\n prenom : ");
gets(prenom2);
if (strlen(nom2) > 0)
{
                 strcpy(p.nom, nom2);
                 strcpy(p.prenom, prenom2);
                 printf("\n age :  ");
                 scanf("%d", &p.age);
                 printf("\n taille en cm : ", &p.taille);
                 scanf("%d", &p.taille);
                 printf("\n vous avez encodez : %s\t%s\t%d\t%d", p.nom, p.prenom, p.age, p.taille);
                 fflush(stdin);
                 tableau[i++]=p;
}
                     
}
while((strlen(nom2)>0)&&(i<4));  
 
nbpers=i;
for (i=0; i<nbpers; i++)
printf("\n ENCODAGE : %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, tableau[i].age, tableau[i].taille);
 
printf("\n Encodez le nom du fichier :");
gets(nomfichier);
 
fichier=fopen(nomfichier,"w");
for (i=0; i<nbpers; i++)
fprintf(fichier, "\n  %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, tableau[i].age, tableau[i].taille);
fclose(fichier);
 
fichier=fopen(nomfichier,"r");
for (i=0; i<nbpers; i++)
fscanf(fichier, "\n  %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, &tableau[i].age, &tableau[i].taille);
fclose(fichier);
 
 
 
ftrinom(tableau, nbpers);
printf("=========TRI SUR NOM=========");
for (i=0; i<nbpers; i++)
printf("\n%s\n", tableau[i].nom);
 
ftriage(tableau, nbpers);
printf("=========TRI SUR AGE=========");
for (i=0; i<nbpers; i++)
printf("\n%d\n", tableau[i].age);
 
 
 
  system("PAUSE");     
  return 0;
}
//fonction tri sur nom
void ftrinom(pers *t, int nbis)
{
 
int sw,i;
pers xt;
 
do
{
sw=0;
for (i=0;i<nbis-1;i++)
if(strcmp(t[i].nom,t[i+1].nom)>0)
{
      xt=t[i];
      t[i]=t[i+1];
      t[i+1]=xt;
      sw=1;
}
}
while (sw==1);
 
}
 
//fonction tri sur age
void ftriage(pers *t, int nbis)
{
int sw, i;
pers xt;
 
do
{
sw=0;
for (i=0;i<nbis-1;i++)
if (t[i].age > t[i+1].age)
{
      xt=t[i];
      t[i]=t[i+1];
      t[i+1]=xt;
      sw=1;
}
}
while (sw==1);
 
}


le passage que je ne comprend pas est celui-ci.

fichier=fopen(nomfichier,"w");
for (i=0; i<nbpers; i++)
fprintf(fichier, "\n  %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, tableau[i].age, tableau[i].taille);
fclose(fichier);
 
fichier=fopen(nomfichier,"r");
for (i=0; i<nbpers; i++)
fscanf(fichier, "\n  %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, &tableau[i].age, &tableau[i].taille);
fclose(fichier);


Merci d'avance pour vos interventions
Configuration: Windows XP
Firefox 2.0.0.12

2 réponses

  1. TSniper Messages postés 796 Statut Membre 157
     
    fichier=fopen(nomfichier,"w");// l'ouverture du fichier nomfichier en mode écriture
    for (i=0; i<nbpers; i++)//la saisie du contenu par une boucle for
    fprintf(fichier, "\n %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, tableau[i].age, tableau[i].taille);
    fclose(fichier);//la fermeture du fichier est obligatoire apres traitement

    fichier=fopen(nomfichier,"r");//l'ouverture du fichier nomfichier en mode lecture
    for (i=0; i<nbpers; i++)//l'affichage du contenu
    fscanf(fichier, "\n %s\t%s\t%d\t%d", tableau[i].nom, tableau[i].prenom, &tableau[i].age, &tableau[i].taille);
    fclose(fichier);//la fermeture du fichier est obligatoire apres traitement
    0
  2. limbergh Messages postés 669 Statut Membre 140
     
    ok, merci pour ta réponse...
    0
    1. TSniper Messages postés 796 Statut Membre 157
       
      de rien pa de koi.
      0