CKTIS / C
SEEK_SET, SEEK_CUR, SEEK_END — ishodišta pozicioniranja
Makronaredbe SEEK_SET, SEEK_CUR i SEEK_END odabiru referentnu točku za fseek: početak, trenutačni položaj ili kraj datoteke.
Verzija: C23; repository compiler flag, production runtime not attested
Sintaksa
#include <stdio.h>
int fseek(FILE *stream, long int offset, int whence);
/* whence: SEEK_SET, SEEK_CUR, SEEK_END */Ove makronaredbe iz <stdio.h> imaju različite cjelobrojne vrijednosti; nemojte pretpostavljati određene brojeve. Argument whence odabire osnovu: SEEK_SET — početak datoteke, SEEK_CUR — trenutačni položaj, SEEK_END — kraj datoteke.
Za binarni tok fseek dodaje offset toj osnovi, mjereno u C bajtovima. Podrška za SEEK_END nije zajamčena čak ni za binarne tokove. Za tekstni tok offset mora biti nula ili potjecati od prethodnog uspješnog poziva ftell za istu datoteku; u potonjem je slučaju potreban SEEK_SET.
Provjerite rezultat funkcije fseek: nula znači uspjeh, a vrijednost različita od nule neuspjeh.
Primjeri
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *f = tmpfile();
int a, b;
if (f == NULL) return EXIT_FAILURE;
if (fputs("ABCDE", f) == EOF ||
fseek(f, 1L, SEEK_SET) != 0) {
fclose(f);
return EXIT_FAILURE;
}
a = fgetc(f);
if (a == EOF || fseek(f, 1L, SEEK_CUR) != 0) {
fclose(f);
return EXIT_FAILURE;
}
b = fgetc(f);
if (b == EOF || fclose(f) != 0) return EXIT_FAILURE;
if (printf("%c %c\n", a, b) < 0) return EXIT_FAILURE;
return EXIT_SUCCESS;
}Rezultat
B D
Prikazani kôd i navedeni rezultat provjereni su u izoliranom okruženju metodom zabilježenom u dokazu provjere za ovu točnu verziju.