__freadahead() — Retrieve number of bytes remaining in input buffer

Standards

Standards / Extensions C or C++ Dependencies
Language Environment both  

Format

#include <stdio.h>
#include <stdio_ext.h>

size_t __freadahead(FILE *stream);

#define _OPEN_SYS_UNLOCKED 1
#include <stdio.h>
#include <stdio_ext.h>

size_t __freadahead_unlocked(FILE *stream);

General description

The __freadahead() function retrieves the number of bytes remaining to be read in the input buffer that is associated with the specified binary stream. When the stream is opened for text processing, __fpending retrieves the number of characters pending to be written.

The __freadahead_unlocked() function is equivalent to the __freadahead() function with the exception that it is not thread-safe. This function can be safely used in a multithreaded application if it is called while the invoking thread owns the (FILE *) object, such as after a successful call to either the flockfile() or ftrylockfile() function.

Usage notes

  1. If __freadahead() is called when the stream is currently writing, __freadahead() returns 0 and sets errno to a nonzero value.
  2. If __freadahead() is called on a data set opened type=record, __freadahead() returns 0 and sets errno to a nonzero value.
  3. For text data sets, any newline characters representing record boundaries will be included in the returned value.

Returned value

The __freadahead() functions return the number of bytes or characters remaining to be read in the current buffer, depending on the type of stream. Otherwise, the __freadahead() functions return 0. If an error has occurred, __freadahead() functions return 0 and set errno to nonzero.

When the stream is wide-oriented text, the __freadahead() function returns a value measured in wide characters.

An application wishing to check for error situations should set errno to 0, then call __freadahead(), and then check errno. If errno is nonzero, assume that an error has occurred.

Error Code
Description
EBADF
The stream specified by stream is not valid.

Example

CELEBF89
/* CELEBF89

   This example writes and reads data to a file while querying the
   stream for information about data in the I/O buffer.

*/

#include <stdio.h>
#include <stdio_ext.h>

void main() {
   FILE *f;
   char filename[FILENAME_MAX] = "myfile.dat";
   char data[128] = "There are 34 bytes in this buffer\n";
   int datalen = strlen(data);
   size_t n = 0;

   f = fopen(filename,"wb+");
   if (f == NULL) {
      perror("fopen() failed\n");
      return;
   }

   if (__fwritable(f)) printf("Writing is allowed on the open stream\n");
   if (__freadable(f)) printf("Reading is allowed on the open stream\n");

   n = fputs(data,f);
   if (n == EOF) {
      perror("fputs() failed\n");
      return;
   }

   n = __fpending(f);
   printf("There are %d bytes in the buffer pending to be written\n", n);

   if (__fwriting(f)) printf("The last operation on the stream was a write\n");

   rewind(f);

   n = fgetc(f);

   n = __freadahead(f);
   printf("There are %d bytes remaining to be read from the buffer\n", n);

   if (__freading(f)) printf("The last operation on the stream was a read\n");

   return;
}
Output
Writing is allowed on the open stream                  
Reading is allowed on the open stream                  
There are 34 bytes in the buffer pending to be written 
The last operation on the stream was a write           
There are 33 bytes remaining to be read from the buffer
The last operation on the stream was a read

Related information