VMS Help  —  CRTL  fgets
    Reads a line from the specified file, up to one less than the
    specified maximum number of characters or up to and including the
    new-line character, whichever comes first. The function stores
    the string in str.

    Format

      #include  <stdio.h>

      char *fgets  (char *str, int maxchar, FILE *file_ptr);

1  –  Function Variants

    The fgets function has variants named _fgets32 and _fgets64 for
    use with 32-bit and 64-bit pointer sizes, respectively.

2  –  Arguments

 str

    A pointer to a character string that is large enough to hold the
    information fetched from the file.

 maxchar

    The maximum number of characters to fetch.

 file_ptr

    A file pointer.

3  –  Description

    The fgets function terminates the line with a null character
    (\0). Unlike gets, fgets places the new-line character that
    terminates the input line into the user buffer if more than
    maxchar characters have not already been fetched.

    When the file pointed to by file_ptr is opened in record
    mode, fgets treats the end of a record the same as a new-line
    character, so it reads up to and including a new-line character
    or to the end of the record.

4  –  Return Values

    x                  Pointer to str.
    NULL               Indicates the end-of-file or an error. The
                       contents of str are undefined if a read error
                       occurs.

5  –  Example

        #include <stdio.h>
        #include <stdlib.h>
        #include <unixio.h>

        main()
        {
            FILE *fp;
            char c_ptr[130];

            /* Create a dummy data file  */

            if ((fp = fopen("file.dat", "w+")) == NULL) {
                perror("open");
                exit(1);
            }

            fprintf(fp, "this is a test\n") ;
            fclose(fp) ;

            /* Open a file with some data -"this is a test"   */

            if ((fp = fopen("file.dat", "r+")) == NULL) {
               perror("open error") ;
                exit(1);
            }

            fgets(c_ptr, 130, fp);
            puts(c_ptr);        /* Display what fgets got.  */
            fclose(fp);

            delete("file.dat") ;
        }
Close Help