Page 164 - C-Language
P. 164

{
             int c;
             while (EOF != (c = getc(fin)))
             {
               /* Note we read a character. */
               num_read++;

               /* Reallocate the buffer if we need more room */
               if (num_read >= *pn)
               {
                 size_t n_realloc = *pn + ALLOCSTEP;
                 char * tmp = realloc(*pline_buf, n_realloc + 1); /* +1 for the trailing NUL. */
                 if (NULL != tmp)
                 {
                   /* Use the new buffer and note the new buffer size. */
                   *pline_buf = tmp;
                   *pn = n_realloc;
                 }
                 else
                 {
                   /* Exit with error and let the caller free the buffer. */
                   return -1;
                 }

                 /* Test for overflow. */
                 if (SSIZE_MAX < *pn)
                 {
                   errno = ERANGE;
                   return -1;
                 }
               }

               /* Add the character to the buffer. */
               (*pline_buf)[num_read - 1] = (char) c;

               /* Break from the loop if we hit the ending character. */
               if (c == '\n')
               {
                 break;
               }
             }

             /* Note if we hit EOF. */
             if (EOF == c)
             {
               errno = 0;
               return -1;
             }
           }

           /* Terminate the string by suffixing NUL. */
           (*pline_buf)[num_read] = '\0';

           return (ssize_t) num_read;
         }

         #endif


        Open and write to a binary file




        https://riptutorial.com/                                                                             140
   159   160   161   162   163   164   165   166   167   168   169