Positionnement
int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
| Constante |
Signification |
| SEEK_SET |
Depuis le debut |
| SEEK_CUR |
Depuis position courante |
| SEEK_END |
Depuis la fin |
Acces direct vs sequentiel
- Sequentiel : lecture lineaire
- Direct : fseek permet de sauter a n'importe quelle position
Recherche dichotomique dans un fichier
int binary_search_file(FILE *f, int target, int lo, int hi) {
while (lo <= hi) {
int mid = (lo + hi) / 2, val;
fseek(f, mid * sizeof(int), SEEK_SET);
fread(&val, sizeof(int), 1, f);
if (val == target) return mid;
if (val < target) lo = mid + 1;
else hi = mid - 1;
}
return -1;
}