__fwriting() — ストリームに対する直前の操作が書き込み操作かどうかの判別

標準

標準/拡張機能 C/C++ 依存項目
Language Environment 両方  

形式

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

int __fwriting(FILE *stream);

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

int __fwriting_unlocked(FILE *stream);

機能説明

__fwriting() 関数は、指定されたストリームに対する直前の操作が書き込み操作かどうか、あるいは指定されたストリームが書き込み専用または付加専用でオープンされているかを判別します。

__fwrite_unlocked() 関数は、スレッド・セーフでないことを除いて、__fwriting() 関数と同等です。この関数は、呼び出し側のスレッドが (FILE *) オブジェクトを所有している間 (flockfile() または ftrylockfile() 関数の呼び出しが正常に実行された後など) に呼び出すと、マルチスレッド・アプリケーションでも安全に使用できます。

戻り値

__fwriting() 関数は、直前の操作が書き込み操作であるか、ストリームが書き込み専用または付加専用でオープンされていた場合、ゼロ以外の値を戻します。それ以外の場合、__fwriting() 関数は 0 を戻します。エラーが発生した場合、__fwriting() 関数は 0 を戻し、errno をゼロ以外の値に設定します。

エラー状態を検査するアプリケーションは、errno を 0 に設定し、__fwriting() を呼び出してから、errno を検査する必要があります。errno がゼロ以外である場合、エラーが発生したと想定されます。

エラー・コード
説明
EBADF
stream によって指定されたストリームは無効です。

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;
}
出力:
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

関連情報