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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| #include <stdio.h>
#include <stdlib.h>
struct nombre
{
int nombre;
char mot [30];
};
void initi ();
void fmenu ();
void ajout ();
void affichage ();
int main()
{
initi();
fmenu();
return 0;
}
void fmenu()
{
int choix;
do
{
printf("\n\n\t\tMENU\n");
printf("===============================\n");
printf("0: exit \n");
printf("1: ajout du nombre\n");
printf("2: affichage du nombre\n");
printf("_____________________________\n");
printf("\tchoix: ");
fflush(stdin);
scanf("%d",&choix);
system("cls");
switch (choix)
{
case 1:
ajout();
break;
case 2:
affichage();
break;
default:
printf("entrer invalide");
}
}while (choix != 0);
system("cls");
}
void initi()
{
FILE *fp;
int pto = -1, ptl = 2 * sizeof(int);
fp = fopen("test.dat", "r+b");
if(fp == NULL)
{
fclose(fp);
fp = fopen("test.dat", "wb");
if(fp != NULL)
{
fwrite(&pto, sizeof(int), 1, fp); // ECRITURE PTO
fwrite(&ptl, sizeof(int), 1, fp); // ECRITURE PTL
}
else printf("\n\t[ERREUR] Echec de la creation du fichier.");
}
fclose(fp);
}
void ajout()
{
struct nombre nouv, ancien;
int pto, ptl, offset = -1, recherche, trouve=0, prec, pos;
FILE *fp;
fp = fopen("test.dat", "r+b");
fread(&pto, sizeof(int),1,fp);
fread(&ptl, sizeof(int),1,fp);
recherche = pto;
printf("entrer votre nouveau nombre: ");
fflush(stdin);
scanf("%d", &nouv.nombre);
fseek(fp, ptl, SEEK_SET);
pos = ftell(fp);
fwrite(&nouv, sizeof (struct nombre), 1, fp);
fwrite(&offset, sizeof (int), 1, fp);
//ce positionne en premier car rien écrit
if(pto == -1)
{
pto = ptl;
ptl = ftell(fp);
fseek(fp, 0, SEEK_SET);
fwrite(&pto, sizeof(int), 1, fp);
fwrite(&ptl, sizeof(int), 1, fp);
}
else
{
//recherche le positionnenment
while (recherche != -1 && trouve == 0)
{
fseek(fp, recherche, SEEK_SET);
fread(&ancien, sizeof (struct nombre), 1, fp);
if (ancien.nombre > nouv.nombre)
{
trouve = 1;
}
else
{
prec = recherche;
fread(&recherche, sizeof (int), 1, fp);
}
}
//en dernier car le nombre est plus grands que tous
if (trouve == 0)
{
pos = ftell(fp);
fseek(fp, prec + sizeof(struct nombre), SEEK_SET);
fwrite(&pos, sizeof(int), 1, fp);
fseek(fp, pos + sizeof(struct nombre) + sizeof (int) ,SEEK_SET);
ptl = ftell(fp);
fseek(fp, sizeof (int), SEEK_SET);
fwrite(&ptl, sizeof(int),1,fp);
}
else
{
//au milieu
if (recherche != pto)
{
fseek(fp, prec + sizeof (struct nombre), SEEK_SET);
fwrite(&pos, sizeof (int), 1, fp);
fseek(fp, pos + sizeof (struct nombre), SEEK_SET);
fwrite(&recherche, sizeof(int), 1, fp);
ptl = ftell(fp);
fseek(fp, sizeof (int), SEEK_SET);
fwrite(&ptl, sizeof(int),1,fp);
}
//premier si il y a quelque chose
else
{
fseek(fp, pos + sizeof (struct nombre), SEEK_SET);
fwrite(&pto, sizeof(int), 1, fp);
ptl = ftell(fp);
fseek(fp, 0 , SEEK_SET);
fwrite(&pos, sizeof(int), 1, fp);
fwrite(&ptl, sizeof(int), 1, fp);
}
}
}
fclose(fp);
}
void affichage()
{
struct nombre affiche;
int pto, ptl, offset;
FILE *fp;
fp = fopen("test.dat", "r+b");
fread(&pto, sizeof (int), 1, fp);
fread(&ptl, sizeof (int), 1, fp);
offset = pto;
while (offset != -1)
{
fseek(fp, offset, SEEK_SET);
fread(&affiche, sizeof (struct nombre), 1 ,fp);
fread(&offset, sizeof (int), 1, fp);
printf("nombre: %d\n", affiche.nombre);
}
fclose(fp);
} |
Partager