Varianti

fgets

Da cppreference.com.
< c | io
 
 
File input/output
Funzioni
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Accesso ai file
Original:
File access
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Diretta input / output
Original:
Direct input/output
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
fread
fwrite
Ingresso formattato / uscita
Original:
Unformatted input/output
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Formattato di input / output
Original:
Formatted input/output
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
File di posizionamento
Original:
File positioning
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ftell
fgetpos
fseek
fsetpos
rewind
La gestione degli errori
Original:
Error handling
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
clearerr
feof
ferror
perror
Le operazioni sui file
Original:
Operations on files
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
remove
rename
tmpfile
tmpnam
 
<tbody>

</tbody> <tbody class="t-dcl-rev ">

</tbody><tbody> </tbody>
Elemento definito nell'header <stdio.h>
char* fgets( char* str, int count, FILE* stream );
char* fgets( char* restrict str, int count, FILE* restrict stream );


Legge al massimo count - 1 caratteri dallo stream e li salva nell'array di caratteri puntati da str. La lettura termina alla prima occorrenza di un carattere di nuova riga (in questo caso str conterrà il carattere di nuova riga) o se l'EOF (end-of-file) viene raggiunto. Se la lettura avviene senza errori un carattere nullo viene inserito dopo l'ultimo carattere scritto in str.

Parametri

str - puntatore a un elemento di un array di caratteri
count - numero di caratteri da leggere (tipicamente la lunghezza di str)
stream - file stream da cui leggere i dati

Valore di ritorno

str in caso di successo, puntatore nullo in caso di errore.

Se l'EOF viene raggiunto, l'indicatore di eof dello stream viene settato (vedi feof()). Nel caso in cui nessun bytes viene letto poichè l'EOF è stato raggiunto immediatamente fgets ritorna il puntatore nullo e str non viene alterata (il primo byte non viene sostituito con un carattere nullo).

Se l'errore è stato causato in altro modo, l'indicatore error viene settato per lo stream (vedi ferror(). Il contenuto dell'array puntato da str è indeterminato (ex. potrebbe non contenere il carattere nullo come terminatore)


Note

POSIX richiede che fgets setti errno se avviene un errore di lettura.

Nonostante la specifica nello standard risulti incertra nel caso in cui count <= 1, le implementazioni più comuni fanno

  • Se count < 1, non fare nulla, riportare un'errore,
  • if count == 1,
  • Alcune implementazioni non fanno nulla e riportano un'errore
  • Altre implementazioni non leggono nulla e inseriscono il carattere nullo in str[0]

Esempio

#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
    FILE* tmpf = tmpfile();
    fputs("Alan Turing\n", tmpf);
    fputs("John von Neumann\n", tmpf);
    fputs("Alonzo Church\n", tmpf);
 
    rewind(tmpf);
    
    char buf[8];
    while (fgets(buf, sizeof buf, tmpf) != NULL)
          printf("\"%s\"\n", buf);

    if (feof(tmpf))
       puts("Fine del file raggiunto");
}

Output:

"Alan Tu"
"ring
"
"John vo"
"n Neuma"
"nn
"
"Alonzo "
"Church
"
Fine del file raggiunto


Vedi anche

legge l'input formattato da stdin, un flusso di file o di un buffer
Original:
reads formatted input from stdin, a file stream or a buffer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione) [modifica]
legge una stringa di caratteri da stdin
Original:
reads a character string from stdin
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione) [modifica]
Scrive una stringa di caratteri in un flusso di file
Original:
writes a character string to a file stream
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

(funzione) [modifica]