1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Reponse
{
int id;
char phrase[255];
int humeur;
int apparition;
int occurences;
}Reponse;
int tailleFichier(FILE *file);
void afficherRep(FILE *repF);
int main(int argc, char *argv[])
{
FILE *repF;
//repF=fopen("reponse","rb+");
//if (repF==NULL)
repF=fopen("reponse","wb+");
int sauv;
Reponse reponse;
//Enregistrement de 4 reponses
sscanf("phrase1","%s",(reponse.phrase));
reponse.humeur=11;
reponse.id=1;
fseek(repF,0,SEEK_END);
fwrite(&reponse,sizeof(Reponse),1,repF);
sscanf("phrase2","%s",(reponse.phrase));
reponse.humeur=22;
reponse.id=2;
fseek(repF,0,SEEK_END);
fwrite(&reponse,sizeof(Reponse),1,repF);
sscanf("phrase3","%s",(reponse.phrase));
reponse.humeur=33;
reponse.id=3;
fseek(repF,0,SEEK_END);
fwrite(&reponse,sizeof(Reponse),1,repF);
sscanf("phrase4","%s",(reponse.phrase));
reponse.humeur=44;
reponse.id=4;
fseek(repF,0,SEEK_END);
fwrite(&reponse,sizeof(Reponse),1,repF);
afficherRep(repF);
int taille=tailleFichier(repF);
fseek(repF,0,SEEK_SET);
while(!feof(repF))
{
printf("0 %d - %d \n",sizeof(Reponse),ftell(repF));
fread(&reponse,sizeof(Reponse),1,repF);
reponse.humeur=100+reponse.id; //modification
printf("1 %d - %d \n",reponse.id,ftell(repF));
// Enregistrement
fseek(repF,-sizeof(Reponse),SEEK_CUR);
fwrite(&reponse,sizeof(Reponse),1,repF);
printf("2 %d - %d \n",reponse.humeur,ftell(repF));
printf("Bouclage\n");
fseek(repF,0,SEEK_CUR);
}
afficherRep(repF);
return 0;
}
int tailleFichier(FILE *file)
{
fseek(file,0,SEEK_END);
int taille=ftell(file);
return taille;
}
void afficherRep(FILE *repF)
{
Reponse rep;
int taille=tailleFichier(repF);
fseek(repF,0,SEEK_SET);
while(ftell(repF)!=taille)
{
fread(&rep,sizeof(Reponse),1,repF);
printf("%d - %s - %d \n",rep.id,rep.phrase,rep.humeur);
}
} |
Partager