Créer un fichier de structures

boninou Messages postés 59 Date d'inscription lundi 17 janvier 2005 Statut Membre Dernière intervention 16 mai 2012 - 20 juin 2007 à 23:04
boninou Messages postés 59 Date d'inscription lundi 17 janvier 2005 Statut Membre Dernière intervention 16 mai 2012 - 20 juin 2007 à 23:42
Bonjour,

Je dois développer un bout de code qui me permette de créer et de remplir un fichier "candidats.dat" qui contient des structures de type candidat.

struct Candidat
{
    char prenom[25];
    char nom[25];
    char parti[10];
    int rep[11];
};

Pouvez vous m'aider svp.

2 réponses

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
20 juin 2007 à 23:15
Candidat cdd;
Remplir champs du candidat, puis

fwrite(&cdd, sizeof(Candidat), 1, file);

fread du meme genre mais penser à ajouter ensuite:
prenom[24] = 0;
nom[24] = 0;
parti[9] = 0;
sait-on jamais que le fichier serait abimé, évitera plantage.

ciao...
BruNews, MVP VC++
0
boninou Messages postés 59 Date d'inscription lundi 17 janvier 2005 Statut Membre Dernière intervention 16 mai 2012
20 juin 2007 à 23:42
Merci pour ta réponse !
Voila se que j'ai fait :

Ecriture :

#include <stdlib.h>
#include <stdio.h>
struct Candidat
{
    char prenom[25];
    char nom[25];
    char parti[10];
    int rep[11];
};
int main()
{
    int j,i=0;
    struct Candidat cp;
    FILE *pf;
    pf=fopen("candidats.dat","ab");
    while(i<2)
    {
        printf("Veuillez saisir les infos \n");
        printf("le prenom \n");
        scanf("%s",&cp.prenom);
        printf("le nom \n");
        scanf("%s",&cp.nom);
        printf("le parti \n");
        scanf("%s",&cp.parti);

        for(j=0;j<11;j++)
        {
           printf("la reponse %d \n",j+1);
           scanf("%d",&cp.rep[j]);
        }

        fseek(pf,0,SEEK_END);
        fwrite(&cp,sizeof(struct Candidat),1,pf);
        i++;
    }
        fclose(pf);
}

J'ajoute don 2 structures candidat dans mon fichier.
Ensuite lecture :

#include <stdio.h>
#include <stdlib.h>
struct Candidat
{
    char prenom[25];
    char nom[25];
    char parti[10];
    int rep[11];
};
int main()
{
    FILE *pf;
    int i,j=0;
    pf=fopen("candidats.dat","rb");
    fseek(pf,0,SEEK_SET);
    while(j<2)
    {
        struct Candidat c;
        fread(&c,sizeof(struct Candidat),1,pf);
        printf("%s\n",c.nom);
        printf("%s\n",c.prenom);
        printf("%s\n",c.parti);

        for(i=0;i<11;i++)
        {
            printf("%d",c.rep[i]);
        }
        j++;
        printf("\n");
    }
    fclose(pf);
}

Affichage de mes 2 structures nickel !

Mais maintenant le vrai pb c'est que j'ai besoin d'utiliser ça dans une appli. Lorsque je met le meme code de lecture dans ma procédure de mon appli les valeurs affichées n'ont aucun sens !!!

Voici mon programme principal :

#include <stdio.h>
#include <stdlib.h>
#include <conio2.h>
#include <mem.h>

//Structure Candidat
struct Candidat
{
    char prenom[25];
    char nom[25];
    char parti[20];
    int rep[11];
};

//Procédure qui permet de patienter
void wait()
{
    char c;
    fflush(stdin);
    printf("Appuyer sur une touche pour continuer !");
    scanf("%c",&c);
}

//Procédure qui lance le questionnaire
void lancer(int reponses[])
{
    clrscr();
    FILE *q;
    FILE *s;
    char ch[255];
    int i=0;
    int max;
    q=fopen("questions.txt","r"); //On ouvre le fichier questions.txt en mode lecture
    s=fopen("saverep.dat","ab"); //On ouvre le fichier saverep.dat en mode création et écriture
    fseek(q,0,SEEK_SET);
    while(!feof(q)) //Tant qu'on n'est pas à la fin du fichier
    {
        clrscr();
        fflush(stdin);
        fgets(ch,255,q); //On recupere la question
        gotoxy(9,5);printf("%s",ch); //On l'affiche
        gotoxy(9,7);printf("1. Pour");
        gotoxy(35,7);printf("3. Plutot pour");
        gotoxy(9,8);printf("2. Contre");
        gotoxy(35,8);printf("4. Plutot contre");
        gotoxy(9,10);scanf("%d",&reponses[i]); //On enregistre la réponse
        i++;
    }
    fseek(s,0,SEEK_END);
    fwrite(reponses,sizeof(int),11,s);
    fclose(q);
    fclose(s);
}

//Procédure qui compare les réponses aux réponses des candidats
int compare(int reponses[], int comp[])
{

    FILE *pf;
    int i,j=0;
    pf=fopen("candidats.dat","rb");
    fseek(pf,0,SEEK_SET);
    while(j<2)
    {

        struct Candidat c;
        fread(&c,sizeof(struct Candidat),1,pf);
        printf("%s\n",c.nom);
        printf("%s\n",c.prenom);
        printf("%s\n",c.parti);

        for(i=0;i<11;i++)
        {
            printf("%d",c.rep[i]);
        }
        j++;
        printf("\n");
    }
    fclose(pf);
    /*int i=0,j,k,max=0,z;
    struct Candidat c[2];
    FILE *r;
    for(z=0;z<2;z++)
    {
        comp[z]=0;
    }
    r=fopen("candidats.dat","rb"); //On ouvre le fichier en mode lecture
    fseek(r,0,SEEK_SET);
    fread(c,sizeof(struct Candidat),2,r);
    while(i<2) //Tant qu'on n'a pas comparer tous les candidats
    {
        for(j=0;j<11;j++)
        {
           printf("%d",c[i].rep[j]);
            if(c[i].rep[j]==reponses[j])
            {
                 comp[i]=comp[i]+1;
            }
        }
        if(comp[i]>comp[max])
        {
            max=i;
        }
        else if(comp[i]==comp[max] && max!=0)
        {
            //affinage
        }
       i=i+1;
       printf("\n");
    }
    fclose(r);
    return(max);*/
}

/*void affiche(max)
{
    struct Candidat c[];
    FILE *r;
    r=fopen("candidats.dat","rb"); //On ouvre le fichier en mode lecture
    fseek(r,max*sizeof(struct Candidat),SEEK_SET);
    fread(&c,sizeof(struct Candidat),1,r);
    printf("Le Gagnant est : %s %s (%s)\n",c.prenom,c.nom,c.parti);
}*/

int main()
{
    int choix,retour;
    int rep[11];
    int comp[2];
    int i, vainqueur;
    for(i=0;i<11;i++)
    {
        rep[i]=0;
    }
    do
    {
        clrscr();
        gotoxy(9,5);
        printf("De quel candidat a la presidentielle etes-vous le plus proche ?");
        gotoxy(20,10);
        printf("1 ... Lancer le questionnaire");
        gotoxy(20,12);
        printf("2 ... Voir les reponses des candidats");
        gotoxy(20,14);
        printf("3 ... Statistiques");
        gotoxy(20,16);
        printf("4 ... Quitter");
        gotoxy(20,18);
        scanf("%d",&choix);

        switch(choix)
        {
            case 1:
                lancer(rep);
                vainqueur=compare(rep,comp);
                //printf("%d\n",comp[0]);
                //printf("%d\n",comp[1]);
               // printf("%d",vainqueur);
                wait();
                break;
            case 2:
            break;
            case 3:
            break;
        }
    }
    while(choix!=4);
}

ne pas tenir compte de ce qui est entre /*  */ je fais des test c'est pour ça.

Désolé pour tout ce code mais c'est nécessaire pour que tu cernes mon pb.

Merci de prendre le temps de me lire et de me répondre.
Je bloque sur ce probleme depuis 2 jours!!!
C'est mon projet de fin d'année....

Merci beaucoup
0
Rejoignez-nous