fgets

Gets a string of characters from a stream and stores them in an array

Local Servers:blocking
Remote Servers:blocking
Classification:ANSI
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h>  
   
  char *fgets  (  
     char     *buf,   
     size_t    n,   
     FILE     *fp);
  

Parameters

buf
(OUT) Points to the array into which the characters are to be stored.
n
(IN) Specifies the number of characters to read.
fp
(IN) Points to the file to be read.

Return Values

The fgets function returns buf if successful. NULL is returned if end-of-file is encountered or if a read error occurs. If an error occurs, errno is set.

If you link your application to the PRELUDE.OBJ file, the first character in the buffer is set to zero (0).

If you link your application to the NWPRE.OBJ file, the buffer passed to fgets is left untouched if the return is set to zero (0).

Remarks

The fgets function gets a string of characters from the file designated by fp and stores them in the array pointed to by buf. The fgets function stops reading characters when end-of-file is reached, or when a newline character is read, or when n-1 characters have been read, whichever comes first. The newline character is not discarded. A NULL character is placed immediately after the last character read into the array.

The gets function is similar to fgets except that it operates with stdin ; it has no size argument, and it replaces a newline character with the NULL character.

For backward compatibility with versions of CLIB.NLM previous to version 4.11, applications should link to PRELUDE.OBJ. For ANSI or POSIX compliance, applications should link to NWPRE.OBJ. Applications cannot link to both.

See Also

fgetc, fopen, getc, gets

fgets Example

  #include <stdio.h>  
   
  main ()  
  {  
     FILE   *fp;  
     char    buffer[80];  
     fp = fopen ("data.fil", "r");  
     while (fgets (buffer, 80, fp) != NULL)  
        fputs (buffer, stdout);  
     fclose (fp);  
  }