Dalla A alla Z passando per C - Robotica
Dalla A alla Z passando per C - Robotica
Dalla A alla Z passando per C - Robotica
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
può creare problemi, dal momento che la fscanf legge dati fintanto che non trova un EOF o<br />
un carattere di andata a capo. Quindi, indipendentemente d<strong>alla</strong> dimensione di buff, verranno<br />
copiati in buff stesso tanti caratteri quanti sono quelli letti, causando <strong>per</strong> l’appunto un overflow<br />
del buffer stesso. Il problema nasce dal fatto che la fscanf non effettua nessun controllo sul<br />
numero di caratteri letti.<br />
Per ovviare a tale problema, il codice precedente può essere sostituito con<br />
{<br />
}<br />
char buff[BUFSIZ], st[BUFSIZ];<br />
if ((fgets(buff, sizeof(buff), fin) == NULL) ||<br />
sscanf (buff, "%s", st) != 1)<br />
{<br />
/* ... */<br />
}<br />
A differenza di fscanf, la fgets riceve come argomento il numero massimo di caratteri da<br />
leggere, e quindi non rischia - se utilizzata correttamente come nell’esempio - di provocare overflow.<br />
Infatti, nel buffer buff saranno memorizzati al più sizeof(buff), senza rischio di causare<br />
overflow.<br />
130